c

solving default garbage values in c

If we declare a variable in c programming as integer without defining value then printf prints some garbage value from buffer. Is there any method to prevent printing the garbage value? I want to check if something can be done at compile time? if possible? ...

Can I have an address family independent network packet struct?

I am adding IPv6 support to a network application. Some control packets should contain a mixed list of both IPv4 and IPv6 addresses (the current code only supports IPv4): struct control_packet { [...] ushort ip1_afi; struct in_addr ip1; ushort ip2_afi; struct in_addr ip2; uchar reser...

Rotated 2d rectangle intersection detection

I have 2 rectangles they are placed arbitrarily and I have rects all four corner point like struct Rect { NSPoint topLeft; NSPoint topRight; NSPoint bottomLeft; NSPoint bottomRight; } I want to check whether 2 rectangles intersects. I am looking a method similar to NSIntersectsRect . But NSIntersectsRectwon't respect the rota...

Printing a C string in reverse without using pointers?

Is there a way to print a string of fixed size in reverse without using pointers? #include<stdio.h> main() { char buffer[10]; scanf("%s", buffer); // need to print buffer in reverse without using pointers?? } ...

Sending structured data over named pipe (Linux)

Hi, i am using a named pipe for IPC on a Debian system. I will be sending some data as a set of strings from a bash script to a background running process written in C code. The data i want to send is four strings eg accountid, firstname,surname, description. Currently i am sending the data as a char array separated by spaces from my ba...

Does the return process cast/convert the type required to a result according to the C standard, or is that only supported in certain implementations?

I noticed that float f; [..] printf("%i\n", f); is not reasonable but printf("%i\n", (int)f); is but also int func(float f) { return f; } is ok on printf("%i\n", func(f)); Is that conversion/casting that is being done by the return process or the function supported by the standard, or does it ideally need int func(fl...

pointers to pointers and pointer arrays

Hello, gcc 4.4.4 c89 I understand pointers ok. However, I am stepping up to pointer arrays and pointers to pointers. I have been messing around with this code snippet and have left comments of what I think I understand. Many thanks for any advice if my comments are correct with the line of code? void increment_ptr() { /* Static ...

Argument of type "volatile char *" is incompatible with parameter of type "const char *"

I have a function whose prototype is as follows: void foo(const char * data); Elsewhere in my code, I have a global variable declared as follows volatile char var[100]; Whenever I try to do this: foo(var); The compiler throws up the following error message: Argument of type "volatile char *" is incompatible with parameter of ty...

Simulate serial port

I am writing a C program in Linux which will read/write to/from a serial port. I know the data that needs to be read and written on the port but I don't have a serial port to currently test this with. Is there any way to simulate a serial port? Would reading/writing to a file be sufficient? I can have one process write to the file whil...

MS C Compiler requires declarations of variables to be at the beginning of a function. Is that supported by the standard?

A common error is 'var' : illegal use of this type as an expression ...

What belongs in an educational tool to demonstrate the unwarranted assumptions people make in C/C++?

I'd like to prepare a little educational tool for SO which should help beginners (and intermediate) programmers to recognize and challenge their unwarranted assumptions in C, C++ and their platforms. Examples: "integers wrap around" "everyone has ASCII" "I can store a function pointer in a void*" I figured that a small test program...

snprintf : simple way to force . as radix?

My program was not behaving correctly on one machine so I started to hunt for the bug, and I discovered that on that machine, snprintf uses a comma (,), not a . (dot) as 99% of other computers (at least in my experience). Shouldn't this be standardized? I am using a library that assumes that the radix is a . (dot) and so it does not wo...

How to make a UDP socket replace old messages (not yet recv()'d) when new arrive?

Hello everybody! First, a little bit of context to explain why I am on the "UDP sampling" route: I would like to sample data produced at a fast rate for an unknown period of time. The data I want to sample is on another machine than the one consuming the data. I have a dedicated Ethernet connection between the two so bandwidth is not an...

Why does this program give "stack smashing error" ?

input .txt contents start_point(0) end_point(100) turtle_speed(3) hare_speed(6) hare_sleep_time(5) hare_turtle_dist(20) process_delay(1) stack smashing error occurs if i put hare-TURTLE_DIST GREATER THAN 10 code #include<stdio.h> #include<stdlib.h> void get(int *a,FILE *fp,int i,int j){ char str[19]; int input=0; fs...

How to validate user input?

In the program written below how i can ensure that only integer value is entered? And if character is entered the program should convert it to its ASCII equivalent and then add them and show output as number. Please help me...... #include<stdio.h> int main(int argc, char **argv) { int a,b,c; printf("enter two numbers:-"); scanf("%d \...

Parsing: load into memory or use stream

I'm writing a little parser and I would like to know the advantages and disadvantages of the different ways to load the data to be parsed. The two ways that I thought of are: Load the file's contents into a string then parse the string (access the character at an array position) Parse as reading the file stream (fgetc) The former wil...

object file from .a not included in .so

Hi, I have created a .c file which is being converted to a .o file along with around 300 other .c files and included in a .a static library. This library, along with many others is being used to create a .so dynamic library. On analyzing both the .a and the .so file with nm, I found that for some reason the symbols defined in the .c fil...

How to number my Custom errnos

I return a error code if my program was abnormally terminated (via exit()). For standard situations, I just return the underlying errno (for example, ENOMEM for failed mallocs etc). There are, however, also cases when I'll have to terminate due to my own reasons for which there are no system errnos defined. What error values should I re...

Arrays of pointers in C

Hii , I have written the following code to improve it for the higher datastructures . #include<stdio.h> #include<stdlib.h> #include<malloc.h> int display(int *a , int *b , int *c) { a[0] = b; a[1] = c; printf("\n%d %d",a[0],a[1]); ------- point 1 printf("\n %d %d",*(a[0]),*(a[1])); ------- point 2 re...

Displaying ASCII value of a character in c

I have accepted a character as an input from the user. I want to print the ASCII value of that character as an output. How can I do that without using any pre-defined function (if it exists) for the same? ...