Welcome, Guest. Please login or register.

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

Description:

0 Members and 1 Guest are viewing this topic.

Offline FluffyMcDeath

  • Hero Member
  • *****
  • Join Date: Jun 2002
  • Posts: 3440
    • Show all replies
Re: Stupid bloody C string question...
« 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 FluffyMcDeath

  • Hero Member
  • *****
  • Join Date: Jun 2002
  • Posts: 3440
    • Show all replies
Re: Stupid bloody C string question...
« Reply #1 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 FluffyMcDeath

  • Hero Member
  • *****
  • Join Date: Jun 2002
  • Posts: 3440
    • Show all replies
Re: Stupid bloody C string question...
« Reply #2 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!!!