Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

string and pointer dereferencing

Status
Not open for further replies.

boebox

MIS
Oct 30, 2000
70
CA
Here is my test prog. Why is it that p1 and p2 do not have to be dereferenced?


char *p1 = "Hello";
char *p2 = "World!";

printf("%s %s", p1, p2); ---> *p1 and *p2




Thanks,
Tony


 
Maybe your compiler is calling malloc automatically when it sees this, maybe it is optomizing it as :

char *p1 = "hello" is equiv to char *p1 = new("Hello")

in c++ As always, I hope that helped!

Disclaimer:
Beware: Studies have shown that research causes cancer in lab rats.
 
Hi,
All strings are arrays of char's.
The name of the array is a pointer to the first element of the array.
printf("%s",pntr)(NOTE %s) treats the pointer to char as a string pointer and will output all the char's till it finds a \0.

In other words in the two statements below pntr and array are both pointers.

char *pntr = "Hello";
char array[6] = "Hello"

Hope this helps.
Pappy Pappy
You learn somrthing everyday.
 
Actually, they are not both pointers. An array decays to a pointer in _certain contexts_ (such as when passing an array to a function). But they are wholly different things.

For example, you can't do this:

char a[20];
char *b="hello";

a=b;

But you could do this:

char *a;
char *b="hello";

a=b;

Or, these statements don't yield the same results:

char *a=malloc(20);
char b[20];

printf("sizeof a: %lu\n",(unsigned long)sizeof a);
printf("sizeof b: %lu\n",(unsigned long)sizeof b);

output:

sizeof a: 4
sizeof b: 20

I don't mean to be picky -- your statements would be correct if you qualified them with "in certain contexts," -- I just wanted to clarify that because I think that a lot of confusion with C stems from statements that suggest that arrays and pointers are the same thing; they're not.

At any rate, I don't feel that the OP's question (whatever it really was) has really been answered.

Russ
bobbitts@hotmail.com
 
Ok, I think I understand what was being asked in the 1st place. The OP was wondering why you didn't have to allocate memory (not dereference) for the pointers.

The compiler sets the pointer to point to string literals in the example provided. It's implementation defined as to how the compiler does this and where the actual string literals reside in memory. The caveat here is that you may not modify what these pointers point to.

In fact, if you had these two assignments:

char *p1="hello";
char *p2="hello";

The compiler very well might set them to point to the same region of memory.

As an aside, this is another difference between arrays and pointers:

char *a="hello";
char b[6]="world";

a[0]='M' /* Wrong! Illegal to modify the string */
b[0]='M' /* No problem, the 'w' is changed to 'M' */

Russ
bobbitts@hotmail.com
 
Hi again,
I was wrong.
Arrays and pointers are not the same.

char *a="hello";
char b[6]="world";

As mentioned above (rbobbitt's post) a[0] does not work. Also

++b; // doesn't work

Thanks for the correction.

Pappy
You learn somrthing everyday.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top