• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Help in C programming! (need help with pointers)

wizzz

Senior member
Hello all.
I don't know whether this is a good place to post this kind of things but I don't know anywhere else where the posters are intelligent enough as you guys.

ok...I have a very very serious problem with pointers in C.
I read my book (Efficient C Programming by Weiss) multiple times but I simply cannot understand about pointers.

Like what's the difference betwee int *x; and int* x?

The worst problem I have is when there's a function like
void* int (void). what's that pointer to a void doing?

Also another problem I have is, when I want to return a pointer to an array as the returning value of the function, how can I do it?


Any help is appreciated!
I'd very much appreciate it if someone can really explain to me all about pointers.
 
-------------------


<< Like what's the difference betwee int *x; and int* x? >>



absolutly none...in both cases you are declaring a pointer to an integral value. The parser could care less how many white spaces you have between the type and the instance. for example the following is also valid int * i;.

-------------------


<< void* int (void) >>



void* is a generic pointer (that is a pointer that can be assigned any type and any type can be assigned to the generic pointer. for example

int *i = new int; // alocate i somewhere in memory (lets say address 20F28), so i points to the memory location (20F28)
void *g = NULL; // g points nowhere (NULL)
g = i; // g now points where i is located (20F28)

now lets print there addresses
printf(&quot;%X %X\n&quot;, i, g); // prints 20F28 20F28

you cannot dereference generic pointers, for example...

int *j = *g; // error

but the following is valid

int *j = (int*)g; // now j = 20F28 and *j = 6 becuase the compiler knows the size of the type (int*) in this case;

---------------------
here's an example that returns a pointer to the array

/* pass in float array and return its pointer */

float* rfp(float fa[])
{
return fa;
}


void main( )
{
float fa[32];
// allocate some space for float values

printf(&quot;%X %X\n&quot;, fa, rfp(fa)); // prints the same value (address of the fa) because fa and the return value from rfp(float []) is the same
}
 
Back
Top