Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
05/24/04 08:07
Read: times


 
#71024 - RE: Another stringy problem
Responding to: ???'s previous message
hi,
you know, in C-language there is a difference between array and string pointer. So you should be careful when pass pointers/arrays into functions.

For example, we have two declarations:

char array[] = "abcd";
char *pointer = "abcd";

From first look you may think they are same. But they are not.
First line puts string somewhere in memory and its address is remembered with compiler for future usage as label "array". So, it requires only 5 bytes of memory.
Second line puts string somewhere in memory as well (it takes 5 bytes too). Additionally it reserves some bytes of memory for the pointer labeled "pointer", and initializes it with the address of string. How much bytes are used to storage a pointer depends on compiler/system you work under.

Well, we do:
printf ("%st%sn", array, pointer);

...and there is no problems: printf accepts the remembered address of first byte of array and the value of pointer which is the address of string as well.

But now, imagine next command:

void *vp;

vp = &array;
vp = &pointer;

First line initializes vp with address of the first letter of array (here we may use just vp=array;).
Second line does different: it initializes vp with address of the pointer, i.e. address of memory where this variable is stored (compare with vp=pointer;).

Why this difference from? As it was said: array is just a "label" - a number which represents the address of a string. Compiler remembers it and uses when needs. Opposite it, pointer is the variable placed somewhere in memory and it contains the address of a string placed at another memory location. To understand it, just write:

array=0;
pointer=0;

Second line will be passed with compiler; first one causes error: "There are no conversions to array types, although there are conversions to references or pointers to arrays".

Regards,
Oleg

List of 6 messages in thread
TopicAuthorDate
Another stringy problem            01/01/70 00:00      
   RE: Another stringy problem            01/01/70 00:00      
      RE: Another stringy problem            01/01/70 00:00      
         RE: Another stringy problem            01/01/70 00:00      
         RE: Another stringy problem            01/01/70 00:00      
   RE: Another stringy problem            01/01/70 00:00      

Back to Subject List