Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

browny

difference between [] and *

This topic is 5605 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

is there any difference between the following declares extern char *strVersion; extern char strVersion[]; The reason i''m asking is that is becase i am trying to link some assembly [ assembled with NASM ] code with my C project and in the asm file there was something like [SECTION .data] strVersion db ''Release Version 1.2'',0 Now, when i prototype this with "extern char *strVersion" in the .c file the program crashes when i execute "printf("%s\n",strVersion)" whereas it runs fine when i prototype it with "extern char strVersion[]" What is the difference ? Aren''t they suppose to be the same thing ?

Share this post


Link to post
Share on other sites
Advertisement
Unless there's something in the brackets that you aren't mentioning, then I think they're slightly different. The first is a pointer to nothing in particular (at least until you point it to something). The second is a pointer to an array of unspecified length. It should have some acutal memory allocated for it somewhere else...

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


You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming

You are unique. Just like everybody else.

"Mechanical engineers design weapons; civil engineers design targets."
"Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensor

Yanroy@usa.com

[edited by - Yanroy on August 7, 2003 1:35:52 PM]

Share this post


Link to post
Share on other sites
array name = pointer to first element

If you have...
char hithere[5];

Then...
hithere[2] == *(hithere + 2)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by browny
is there any difference between the following declares

extern char *strVersion;
extern char strVersion[];

The reason i''m asking is that is becase i am trying to link some assembly [ assembled with NASM ] code with my C project and in the asm file there was something like

[SECTION .data]
strVersion db ''Release Version 1.2'',0

Now, when i prototype this with "extern char *strVersion" in the .c file the program crashes when i execute "printf("%s\n",strVersion)" whereas it runs fine when i prototype it with "extern char strVersion[]"

What is the difference ? Aren''t they suppose to be the same thing ?



The difference (I believe) is that the first one is a pointer to a char (possibly to the first element of an array of char) and the second one is an array of char. In C (and C++) there''s a slight (but not insignificant) difference between an array and a pointer to the first element of an array. Why this is making a difference I''m not sure, but it''s the only difference I can see.

Share this post


Link to post
Share on other sites
extern char strVersion[]: Implies that somewhere, in another compilation unit, there is some memory that is an array of chars named strVersion.

extern char* strVersion: Implies that somewhere, in another compilation unit, there is some memory that is a pointer to a location in memory that stores char(s).

Obviously, you've never actually allocated memory for a pointer anywhere, only the chars it would point to. So your C side is seeing strVersion, taking the first four characters of it, and assuming that that's a pointer to somewhere in memory with chars. Mayhem, predictably, ensues.


How appropriate. You fight like a cow.

[edited by - sneftel on August 7, 2003 4:26:20 PM]

Share this post


Link to post
Share on other sites
they are the same if you do not specify a number in the brackets. the [] operator is shorthand for pointer arithmatic. for example :

f[10] == *(f + sizeof(char) * 10); // if f is a char array

if the size of the array is specified then the array is allocated on the stack. using the new operator puts it on the heap (i think).

Share this post


Link to post
Share on other sites
Sneftel is, of course , right.

char x[] = "string"; puts "string" into the .data segment and operations on this will directly reference that data.

char *x = "string"; also puts "string" into the .data segment, but is, itself, merely a pointer to that data, rather than the data itself. (someone with a little more expertise can tell us if the pointer value would be .bss or .data, assuming a global variable, of course. my guess is .bss)

Share this post


Link to post
Share on other sites
I must agree to what Sneftel says. It may be confusing, that pointers and arrays behave similar in many cases, but an array variable is not the same as a pointer. If I am right, this has been discussed in that forum some time ago.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!