2

Bear with me as I am adding some minor, secondary questions too instead of posting those separately

In a declaration char name[]="Germ"; the identifier Germ is of type char[5], right? But in an assignment like ptr="Germ", where ptr had been declared as a character pointer, "Germ" acts as a character pointer, right?

So here are my confusions which I request you to clear. Even one liners would be helpful:

1) Why is "Germ" an array object in declaration but a pointer in second? Should I conclude that a particular syntax has different meaning during declaration than in other statements? For example "{'a','b','c'}" is an initializer for an array and not a compound statement even though enclosed in curly brackets...

2) In the first declaration of this question, are "&name" and "name" of different types in that "&name" is the address of an array object of size 5 while "name" is the address of a character variable, i.e, the address of the first element of the array called name? I feel it is so, but want your confirmation.

3) And finally, if modifying strings is UB in C (I read in a good book), how come the following code doesn't show even a warning and prints "ariund"?

#include<stdio.h>

int main()
{
char str[]="around";
str[2]='i';
printf("%s",str);
return 0;
}

You answers will be very much appreciated.

Meathead
  • 493
  • 2
  • 6
  • 15
  • Modifying string literal is invalid but for case `char str[]="around";` it's valid. [see here](http://stackoverflow.com/questions/22703720/i-feel-confusion-about-bus-error-in-string-c/22703925#22703925) – Jayesh Bhoi Sep 14 '14 at 09:56
  • It is not an assignment; it is an initializer for the array. `str` is the name of an array. The array lives in automatic storage. – wildplasser Sep 14 '14 at 09:58
  • @Jayesh umm, in simple and precise terms, how you define a "string literal"? The identifier of the string? – Meathead Sep 14 '14 at 09:59
  • `ptr="Germ"` is assignment make ptr point to string literal which is constant string and it's placed in read only parts. – Jayesh Bhoi Sep 14 '14 at 10:04
  • @user3121023 Oops, typo. Corrected. – Meathead Sep 14 '14 at 10:29
  • http://stackoverflow.com/questions/1011455/is-it-possible-to-modify-a-string-of-char-in-c – Meathead Oct 24 '14 at 10:56

1 Answers1

3

This is not a direct answer to your question, but it might help you to understand some of the issues...


Given the following function:

void func()
{
    char  arr[] = "RW";
    char* ptr   = "RO";
}

During the build process, both strings are hard-coded into a read-only section of the executable image.

During runtime, every time the function is called:

  • The content of the "RW\0" string is copied into the stack (i.e., into arr)
  • The address of the "RO\0" string is copied into the stack (i.e., into ptr)

At this point, since you have a copy of the "RW\0" string, you can change the contents of that copy.

You cannot change either one of the original strings, as they both reside in a read-only memory section.


A few notable differences between arrays and pointers:

With int arr[10]:

  • Amount of memory used is sizeof(int)*10 bytes

  • The values of arr and &arr are necessarily identical

  • arr points to a valid memory address, but cannot be set to point to another memory address

With int* ptr = malloc(sizeof(int)*10):

  • Amount of memory used is sizeof(int*) + sizeof(int)*10 bytes

  • The values of ptr and &ptr are not necessarily identical (in fact, they are mostly different)

  • ptr can be set to point to both valid and invalid memory addresses, as many times as you will

barak manos
  • 29,648
  • 10
  • 62
  • 114
  • You are very close to what I wanted the answer for. Can you please give me an example of a statement that attempts to change either of the original strings? – Meathead Sep 14 '14 at 10:31
  • Your update was very much helpful, but I still couldn't gather an answer for what I asked in the above comment. Can you please give a simple example of statements that attempt to change either of the original strings you illustrated?Please... – Meathead Sep 14 '14 at 10:38
  • @Meathead: In order to change the contents of the original `"RO\0"` string pointed by `ptr`, you can simply do something like `ptr[0] = 'k'`. Of course, that will result with a memory access violation during runtime. – barak manos Sep 14 '14 at 10:39
  • Thank you. It answers my question exactly. – Meathead Sep 14 '14 at 10:40
  • @Meathead: You're welcome, feel free to accept the answer by clicking on the V next to it :) – barak manos Sep 14 '14 at 12:44
  • I would have irrespective :-). I give it some hours just in case other whiz like you has something more to add. Anyways, accepting immediately for you!! – Meathead Sep 14 '14 at 12:54