Welcome, Guest. Please login or register.

Author Topic: C string syntax incoherent ? ...  (Read 2395 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline JoseTopic starter

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 2871
    • Show all replies
C string syntax incoherent ? ...
« on: December 26, 2004, 08:26:03 PM »
I come to this conclusion not because the literature I have says it but by comparing the example code on it doh.., but I'd like you to confirm.
If you assign a string constant to a character array, say
char strg[11] = "C rules!!!";  the compiler automatically assigns each character of the string constant (wich is really an array of characters too) to the array ( yeah I know, it can also be char strg[] = "C rules!!!";)
Now what's confusing is that C treats the strings diferently if they're assigned to a character pointer, for example char *strg = "C rules!!!"; in this case it just assigns a pointer to the string constant.
While this is an exception I think it confuses things because one expects the syntax to be unique! The use of & and * also comes to mind, cause these can be the bitwise and or "the address of", or multiplication or "value at address" dpending on context. However in the literature I've read this is made clear, no problem at all, contrarly to strings!!
Ok enouph of my ranting, I'm just feed up with this book not being more clear about strings, it has confused me for a while :pissed:
But maybe I'm wrong all the way anyway :-?   :lol:  :-D
\\"We made Amiga, they {bleep}ed it up\\"
 

Offline JoseTopic starter

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 2871
    • Show all replies
Re: Stupid bloody C string question...
« Reply #1 on: December 26, 2004, 09:49:55 PM »
Thx, it's confirmed then. I understand it but I still think that the syntax for the char *strg = "C rules!!!" example should be coherent with the rest of the C syntax: char *strg = &"C rules!!!". Maybe I'm just nitpicking but still...
\\"We made Amiga, they {bleep}ed it up\\"
 

Offline JoseTopic starter

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 2871
    • Show all replies
Re: Stupid bloody C string question...
« Reply #2 on: December 26, 2004, 10:41:53 PM »
Thinking better about it I think I just missed out to remember that the array's name without indexing is the address of the first element. If "" is really just a pointer to the first element too ( as the string defenition itself is an array of characters terminated by a null) then by using char *strg = "C rules!!!"; one is assigning a pointer's value to another, & is not necessary like you say.
The other case, char strg[11] = "C rules!!!"; is really where my confusion came from. Since this is indexed I thought it wasn't the same as strg alone (wich is the address of the first element), but it probably works the same cause this is the declaration of the array (not indexing the array to access an element)...
Just forget it... :-)
\\"We made Amiga, they {bleep}ed it up\\"
 

Offline JoseTopic starter

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 2871
    • Show all replies
Re: Stupid bloody C string question...
« Reply #3 on: January 03, 2005, 03:47:42 PM »
@Fats

Yes! That was it :-)
\\"We made Amiga, they {bleep}ed it up\\"