Welcome, Guest. Please login or register.

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

Description:

0 Members and 1 Guest are viewing this topic.

Offline JoseTopic starter

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 2869
    • Show only replies by Jose
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 FluffyMcDeath

  • Hero Member
  • *****
  • Join Date: Jun 2002
  • Posts: 3440
    • Show only replies by FluffyMcDeath
Re: Stupid bloody C string question...
« Reply #1 on: December 26, 2004, 08:54:42 PM »
Quote

Jose wrote:
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.


OK, either way the string is stored as a sequemce of bytes.
So you have "C rules!!!0x00" Where 0x00 is the terminating byte which is always zero.

To use that string in your code you need a way to get an address to the start of it.

If you create it as an array, the address is the array (an array is a pointer to the first element in the array).
This value is constant and you cannot change it so it will always point to the start of the data (though you may change the data unless it is const).

If you use the strng* = "C rules!!!" version then you still have a pointer to the string but you CAN change where this pointer points to so you could lose the start of your string if you mess with the pointer. Sometimes you don't care.

If you have a string that you only wish to print out, for e.g. you can printf("Hello World\n"); which the compiler will turn into the array "Hello World\n" stored somewhere in memory and will generate a call for printf that says printf(char * pointerToString = "Hello World\n") more or less.

If you want to have the start of the string for many uses you can create the string in an array and repeatedly re-use it e.g.

char string[]="Hello World\n";

printf(string);
printf(string);

since the array is just a pointer (well, actually it's an address but we'll ignore the distinction right now for C).

You can even:

char string[] = "Hello World\n";
char* stringPtr = string;

printf(stringPtr);
/* but you can also change what the pointer points to */
stringPointer ++;
printf(stringPtr);

Which would give you the output:

Hello World
ello World
 

Offline Castellen

Re: Stupid bloody C string question...
« Reply #2 on: December 26, 2004, 09:00:27 PM »
Sounds like you're just getting confused by pointers??

The pointer strg only holds a single memory address, the address of the beginning of the string.

While if you had a char string called strg, the string array called strg would hold "C rules!!\0"

Here's one way to demonstrate the use of a pointer:

char *strg = "C rules!!!";   // That's saying put "C rules!!!\0 somewhere in memory, and give strg the address where the "C" is living.

printf("The data at address %d is: %s", strg, *strg);

Should print out something like:
The data at address 5816491 is: C rules!!!


A good way to remember it is * = the data at the address specified by the pointer.
& gets the address of a variable.
 

I think that's right.  Feel free to correct/abuse :-)
It's so confusing to begin with, I still have trouble with pointers!
 

Offline JoseTopic starter

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 2869
    • Show only replies by Jose
Re: Stupid bloody C string question...
« Reply #3 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 FluffyMcDeath

  • Hero Member
  • *****
  • Join Date: Jun 2002
  • Posts: 3440
    • Show only replies by FluffyMcDeath
Re: Stupid bloody C string question...
« Reply #4 on: December 26, 2004, 09:57:19 PM »
Quote

Jose wrote:
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...


The & is intrinsic to the "" since you are alctually asking for initialised storage of a sequence of bytes. I've never really thought about using the & as the " is really its own special bit of syntax.
 

Offline JoseTopic starter

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 2869
    • Show only replies by Jose
Re: Stupid bloody C string question...
« Reply #5 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 FluffyMcDeath

  • Hero Member
  • *****
  • Join Date: Jun 2002
  • Posts: 3440
    • Show only replies by FluffyMcDeath
Re: Stupid bloody C string question...
« Reply #6 on: December 27, 2004, 06:52:21 AM »
You know, you can always do this:

char * strg = "C rules!!";

strg[2] = 'R';

printf(strg);

and get:

C Rules!!!

or, printf(&strg[3]);

to get :

ules!!!

 

Offline Fats

  • Hero Member
  • *****
  • Join Date: Mar 2002
  • Posts: 672
    • Show only replies by Fats
Re: Stupid bloody C string question...
« Reply #7 on: December 27, 2004, 12:03:42 PM »
Quote
The other case, char strg[11] = "C rules!!!"; is really where my confusion came from.


Exactly, I think your confusion comes from the fact that in a variable declaration the = has a different meaning then in a normal C statement.
In a variable declaration the = character is used to initialize the variable and this initialization allows to initialize all the single members of an array.
The = in a statement is an assignment and can only assign a value to one member of an array.

So the following statement is OK:

char test[4] = "foo";

But the following is illegal :

char test[4];

test = "foo";


or

char test[4];

test[4] = "foo";


greets,
Staf.
Trust me...                                              I know what I\'m doing
 

Offline JoseTopic starter

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

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