954,535 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

pointer to a string, question.

Rookie Disclaimer.

I am confused about what is the difference between:

A: char *p="string";
B: char q[10]="string";

For A:
I can do "p++", which gives a pointer points to "t".
But I cannot do command like
"strcat(p, " test")", for example.

For B:

I can do "strcat(q, " test")",
but I cannot do "q++".


Thank you for the precious time!

cocojim
Newbie Poster
10 posts since Jun 2006
Reputation Points: 10
Solved Threads: 0
 

Rookie Disclaimer.

I am confused about what is the difference between:

A: char *p="string";

"string" in this case is an array stored in some global location, and only seven bytes have been allocated for that string. (One is for the character with integer value zero that marks the end of the string.) p is the memory address of that string. By adding one to p, you increase that memory address by the width of one character, so p then contains the memory address of the 't'. You can't strcat onto it because there are only 7 bytes of space for it, and the bytes after the string are used for other information.B: char q[10]="string";

Here, you have a locally stored array, and you're using "string" as an initializer. You can't change the value of q because that's just not allowed in C -- when used in an expression, q is treated as the memory address of the zeroth element of the array. One reason that you can't make q point to something else, other than q, is that sizeof(q) should return 10, the size of the array in bytes, and that would not make sense if it were a general char* style pointer. Compiled code is able to access the contents of the array more direcly (and quickly) because of this restriction.

You can't strcat " test" onto q, either, because q is an array that is 10 characters wide. You're trying to store 12 characters in q, which would overflow its bounds and overwrite other data stored next to the array. If you're lucky, this will result in an error the first time you test it. If you're unlucky, this will result in an error the first time a customer tests it.

Rashakil Fol
Super Senior Demiposter
Team Colleague
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
 

I am confused about what is the difference between:

A: char *p="string"; B: char q[10]="string";

See the diagram in this link . q will be like 6 squares filled with characters from 's' to 'g' and 4 squares filled with 0.

For A: I can do "p++", which gives a pointer points to "t". But I cannot do command like "strcat(p, " test")", for example.


You can do strcat in this case also.For B:
I can do "strcat(q, " test")",
but I cannot do "q++". You can do strcat only if there is enough space left in the array. For example if you defined q as q[7]="string"; , strcat(q, " test") wouldnt work. It will be writing in memory out of limits.
The reason you cant do q++ is because q is constant. You cant change it.

WolfPack
Postaholic
Moderator
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
 
You can do strcat in this case also.
You can't strcat onto it because there are only 7 bytes of space for it, and the bytes after the string are used for other information.


I see contradicting replies in this case. How come? I agree with the part where other information can be overwritten. But is this illegal?

WolfPack
Postaholic
Moderator
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
 

Thank for both of you guy's reply.
But a little further confusion here.

Wolfpack: are you saying "You can do strcat in this case also", meaning for declaring as "char * p= "string"; "?
If so, it doesn't work. It compiles, but if I tried to print out the conctated phrase, I got "Bus Error".

See the diagram in this link . q will be like 6 squares filled with characters from 's' to 'g' and 4 squares filled with 0.

You can do strcat in this case also. You can do strcat only if there is enough space left in the array. For example if you defined q as

q[7]="string"; , strcat(q, " test") wouldnt work. It will be writing in memory out of limits. The reason you cant do q++ is because q is constant. You cant change it.

cocojim
Newbie Poster
10 posts since Jun 2006
Reputation Points: 10
Solved Threads: 0
 

also a quick programming ethics question.
I heard that in principle, when programing I should try to "avoid" array as much as I could, instead try to use pointers as much as I could.
Is this true?

cocojim
Newbie Poster
10 posts since Jun 2006
Reputation Points: 10
Solved Threads: 0
 

Thank for both of you guy's reply. But a little further confusion here.

Wolfpack: are you saying "You can do strcat in this case also", meaning for declaring as "char * p= "string"; "? If so, it doesn't work. It compiles, but if I tried to print out the conctated phrase, I got "Bus Error".

Yep. That is what I am saying.When I did it in C++ and I get an error. I compiled it as a C Program and it executed without any problem. (By the way it is using Visual C, so it maybe because the compiler is non-standard)I also want to clarify this. That was why I answered it like that although I may be incorrect.also a quick programming ethics question.
I heard that in principle, when programing I should try to "avoid" array as much as I could, instead try to use pointers as much as I could.
Is this true?I really don't know. Pointers give me shudders so I use pointers only if I really really have to. But dont quote me on that. I really have no idea.

WolfPack
Postaholic
Moderator
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
 

some further more exloration:

char *p="test";

if i try:

*p="s"; -----error: invalid conversion from 'const char*' to 'char'

if i try:

*p='s';------compiles, but "Bus Error".

So I am thinking:

is it because when one declares
char *p="test"; p is implied as a const?

BTW, I am using the "g++" compiler on my Mac /Unix shell.

Yep. That is what I am saying.When I did it in C++ and I get an error. I compiled it as a C Program and it executed without any problem. (By the way it is using Visual C, so it maybe because the compiler is non-standard)I also want to clarify this. That was why I answered it like that although I may be incorrect.

I really don't know. Pointers give me shudders so I use pointers only if I really really have to. But dont quote me on that. I really have no idea.

cocojim
Newbie Poster
10 posts since Jun 2006
Reputation Points: 10
Solved Threads: 0
 
I see contradicting replies in this case. How come? I agree with the part where other information can be overwritten. But is this illegal?


Well, the behavior is certainly undefined. You don'twant to unpredictably overwrite other variables, do you?

Rashakil Fol
Super Senior Demiposter
Team Colleague
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
 

char *p="test";

if i try:

*p="s"; -----error: invalid conversion from 'const char*' to 'char'

You're trying to assign the memory address of the array, "s", into the zeroth character of the string "test". This isn't an error of const vs. non-const; these are fundamentally different types. You could do p = "s" just fine -- this would modify p so that it contains the memory address of the array "s".is it because when one declares
char *p="test"; p is implied as a const?

More like, "test" is immutable no matter what you assign its memory address to.

What happens when you compile with the option "-Wall" (turn all warnings on)? Does it warn you about char *p = "test"?

Rashakil Fol
Super Senior Demiposter
Team Colleague
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
 

You're trying to assign the memory address of the array, "s", into the zeroth character of the string "test". This isn't an error of const vs. non-const; these are fundamentally different types. You could do p = "s" just fine -- this would modify p so that it contains the memory address of the array "s".

More like, "test" is immutable no matter what you assign its memory address to.

What happens when you compile with the option "-Wall" (turn all warnings on)? Does it warn you about char *p = "test"?


Here is what I got from the compiler:

"test is input for the dynamic link editor, is not relocatable by the static link editor again."

I know that
*p="s";
is a dumb thing to do;
but
*p='s';
is not. The fact it compiles, but always give me "Bus Error", makes me very perplexed.

cocojim
Newbie Poster
10 posts since Jun 2006
Reputation Points: 10
Solved Threads: 0
 
Well, the behavior is certainly undefined. You don't want to unpredictably overwrite other variables, do you?

Nope. I just went through the specs for strcat in MSDN. It says that it does not check for buffer overruns. So that clears the thing with my compiler.

WolfPack
Postaholic
Moderator
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You