0

Possible Duplicate:
What is the difference between char a[] = “string”; and char *p = “string”;

I read this question somewhere.

char buf[]="hello";
char *buf="hello";
In terms of code generation, how do the two definitions of buf, differ?

There were four options to this and I marked

The first definition certainly allows the contents to buf to be safely modified at runtie; the second definition does not.

But as it turns out, the quiz master had the following to be marked as correct.

They do no differ-- they are functionally equivalent

Why is my choice wrong, since I can not do buf[3]='t' for the second case, but I can for the first one?

Thanks.

Community
  • 1
  • 1
Kraken
  • 23,393
  • 37
  • 102
  • 162
  • 6
    Someone who claims that they are not different should definitely not be your C teacher. – Blagovest Buyukliev Oct 24 '12 at 11:19
  • 1
    From a compilers code-generation point of view, the two are just the same. That one can be modified and the other not is nothing a compiler can really check for in all cases., so many simple compilers simply don't. – Some programmer dude Oct 24 '12 at 11:21

1 Answers1

-2

I didn't know about the "life-time" of string literal in C hence my answer is not valid.

My answer:

Pointer types and vector/matrix types are interchangeable as long as memory management has been handled. Under the covers the compiler switches vector/matrix indexing operations into pointer arithmetic.

buf[3] == *(buf+3)

In this specific case the compiler counts the length of the string and allocates (stack) memory for both the vector/matrix as well as the pointer.

In other cases you have to manually allocate memory for the pointer in order to safely use it the way you use the vector/matrix. But syntactically the same applies for the vector/matrix because you have to specify it's length char buf[5] - it's not quite as verbose as manual memory allocation but you do have to specify it.

Edit:

The above answer explains the fact that in most cases pointers and vectors/matrixes are functionally interchangeable but they do have a different underlying structure (pointers need an extra memory location to store the address to).

And in the case of strings literals they are allocated and constant for the entire lifetime of the program which makes pointers to them keep on being valid without using memory allocation.

Community
  • 1
  • 1
Mihai Stancu
  • 15,848
  • 2
  • 33
  • 51
  • Not exactly true. An array also has some space for its content, a pointer don't have any space for the content. – Basile Starynkevitch Oct 24 '12 at 11:19
  • In this specific case the compiler counts the length of the string and allocates enough memory to fit. Because otherwise `char buf[]` with no length specification would be a syntax error if the right hand value would not be a countable item who's length could be deduced by the compiler . – Mihai Stancu Oct 24 '12 at 11:21
  • @BasileStarynkevitch An array defined as `char buf[]` does not have space for its contents, the compiler allocates the needed space after counting the letters of the string literal. It does the same for a `char * buf` if the right hand value is a string literal. We're obviously talking about stack allocations. – Mihai Stancu Oct 24 '12 at 11:27
  • @MihaiStancu: in the case of a pointer (the `char *buf = "abc"` case), the compiler merely allocates a pointer and initializes it to point to the string literal which is usually stored in a read-only area of the process, initialized at program startup. The `char buf[]` case is entirely different in that regard - the string is stored within the `buf` array itself (usually on the stack if it's an automatic variable) and is written there when the declaration is reached at runtime. – Blagovest Buyukliev Oct 24 '12 at 11:35
  • Yes that's true the structure of the pointer implies `buf` is a handler to a memory location that contains *the address of* the string literal. – Mihai Stancu Oct 24 '12 at 11:43
  • 1
    The equivalence of *arrays* and *pointers* is one of the most widely held misconceptions about the C language. True, *array identifiers* do *decay* to a pointer in most contexts, but that doesn't make them pointers. Try `sizeof(buf)` in the two cases and see the difference. – Blagovest Buyukliev Oct 24 '12 at 11:49