1

Maybe I've been up too long today, but I can't figure out why this is working:

int main() {
   struct emf_struct {
      long num;
      char *str;
      double real;
   };

   struct emf_struct emf [10]; 

   emf[1].real= 4.5;
   emf[1].str = "This is a string";
   emf[1].num = 1234567890;
   printf("%d-%s-%f\n", emf[1].num, emf[1].str, emf[1].real);
   return(0);
}

When compiled under Microsoft (just cl filename), the output is:
1234567890-This is a string-4.500000

I could understand it if the struct had used a character array, e.g., char str[32], but this is a pointer to a string and it's a simple assignment of a literal (that has no address) to that pointer.

  • Where did the memory space for the assignment come from? Shouldn't I have had to malloc?
  • Without it, shouldn't the assignment have overwritten the real variable coming after str in the struct? (Packing seemed to have no effect.)

Thanks!


EDIT: C is not my primary programming language. The comments and replies people have provided have helped me realize something: literals have "addresses" maintained by the compiler, something I knew was true for initialization but not for assignments. I thought one needed to do this:

int main() {
   char *foo;
   foo = (char *) malloc(17);
   strcpy(foo, "This is a string");
   printf("%s\n", foo);
   return(0);
}

when in fact all I need do is this:

int main() {
   char *foo;
   foo = "This is a string";
   printf("%s\n", foo);
   return(0);
}
cniggeler
  • 103
  • 1
  • 9
  • 3
    *it's a simple assignment of a literal (that has no address)* -- this assumption is wrong. String literals also have memory associated with them. – Ajay Brahmakshatriya Oct 21 '18 at 03:09
  • Thanks Ajay. So I can understand better, why wouldn't it have to look like, emf[1].str = &"This is a string";? – cniggeler Oct 21 '18 at 03:10
  • "toto" is an array of static storage that "decay" into pointer to char in this case. – Stargateur Oct 21 '18 at 03:10
  • 1
    Possible duplicate of [What is array decaying?](https://stackoverflow.com/questions/1461432/what-is-array-decaying) – Stargateur Oct 21 '18 at 03:11
  • @cniggeler a string literal is an array of characters. When you use an array in assignment it converts to a pointer to the first element in the array. Thus it automatically becomes a pointer to the memory where the string is stored. If you did `&"This is a string"`, that would the address of an array (might have the same value), but it is an entirely different thing. – Ajay Brahmakshatriya Oct 21 '18 at 03:22

1 Answers1

3

Where did the memory space for the assignment come from? Shouldn't I have had to malloc?

The string literal you are assigning allready resides in memory. With

char *foo = "bar";

you just have foo point to that location. The pointer should really be char const* though since string literals are immutable.

Why wouldn't it have to look like, emf[1].str = &"This is a string";?

Because a string literal is an array of chars that (like any array) decays to a pointer under most conditions. Exceptions to array decaying into a pointer?

Swordfish
  • 12,971
  • 3
  • 21
  • 43