Did you know this about C? – part 2

Posted: October 18, 2012 in Technology
Tags: , , , ,

a continuation to the post

const char *p,**p2;
char *c,**c2;
p=c; //no warning
c=p; //warning a const pointer to a non const pointer
c2=p2; //warning incompatible type pointer assignment
p2=c2; //warning incompatible type pointer assignments

Explanation :
p=c -> in this assignment all the attributes of RHS are retained with an extra attribute (const) added hence no warning
c=p -> in this assignment, RHS loses one of its attribute
c2=p2 / p2=c2 -> in this assignment, both c2 and p2 are the pointers to pointers and C does not carry the compatibility of pointers associatively to next level and hence incompatible pointer assignment

if( -1 < (unsigned int) (1) )
printf("ANSI C\n");
printf("K & R C\n");

Output of the above code is : “K & R C”
Acc. to ANSI standards, signedness of the operands is retained while acc. to K & R C, if either operands is unsigned, conversion is unsigned. hence it really becomes a compiler specific problem and hence whenever you want to obtain a consistent result out of these kind of codes, use specific type casted conversions.



Flexible Size Array
In a structure, you can have the last element as a size zero element. It helps to have the flexible sized array.
struct myStruct
int length;
int arr[0]
myStruct_t *ms = malloc (sizeof(myStruct) + size * (sizeof(int)));
ms->length = size;

Here element arr of myStruct can have size no. of elements.
More details:

Happy programming.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s