Why does the same data input method not work for a struct variable in an array?

  • Thread starter Thread starter transgalactic
  • Start date Start date
  • Tags Tags
    Data Input
AI Thread Summary
The discussion centers on the differences in struct initialization and assignment in C/C++. When a struct variable is initialized, such as with "struct human TR = {"Tony", 12, 1.5};", the values are set at the time of memory allocation. However, when trying to assign values to an element of an array of structs, like "robots[0] = {"Lunar Lee", 50};", it fails because this syntax is only valid during initialization. The conversation also highlights the importance of memory allocation for string pointers, noting that failing to allocate memory before using functions like strcpy can lead to errors. Ultimately, the distinction between initialization and assignment is crucial for understanding how to properly work with structs in C/C++.
transgalactic
Messages
1,386
Reaction score
0
when i got a normal struct variable code:
Code:
struct human { 
  char name[50]; 
  int age; 
  float height; 
};

struct human TR = {"Tony", 12, 1.5};   \\this works

but when my object is a part of an array and i take just one object
the same data input method doesn't work
why is that?
Code:
#include <stdio.h>

typedef struct robot ROBOT;

struct robot { 
  char *name; 
  int energy; 
};

int main() {
  int i;
  
  ROBOT robots[3];

  
   robots[0] = {"Lunar Lee", 50};  //this  don't work
 
Technology news on Phys.org
The first is initialization of the struct. The specified data is placed in the struct when the memory is allocated before the program begins execution. The second is an attempt to assign values to the struct during program execution, after memory has been allocated. C and C++ allow you to use that syntax only in initialization. Why, I don't know. Maybe the language designers were simply lazy. :smile:

Note that you have the same situation with arrays.
 
Is there also a second problem with the last piece of code in that name is never allocated any memory, or is the allocation made automatically when a constant string like "lunar lee" is defined.

BTW I'm just checking because I'm primarily a Pascal programmer and only half fluent in C.
Actually the more I think about it the more I seem to remember that constant strings like that are automatically assigned memory. But say he used name as a target for strcpy in the second bit of code without assigning memory to it. Then he'd be in trouble right?
 
Last edited:
uart said:
Actually the more I think about it the more I seem to remember that constant strings like that are automatically assigned memory.
That's right. Consider this code:
Code:
const char * name;
double radius, diam;
...
name = "Circle";
diam = 3.14159265358979323846 * radius;
...
The compiled code will contain data sections that include the string "Circle" and the number 3.14159265358979323846 (in the machine's native form for doubles, of course).
But say he used "name" as a target for strcpy in the second bit of code without assigning memory to "name". Then he'd be in trouble right?
That is also correct. Suppose a chunk of code defines two variables char *string1, *string2;. The statements strcpy (string1, "Tony"); and string2="Tony"; do very different things. The first statement does not change the value of string1; it instead changes what string1 points to. If string1 doesn't point to a valid chunk of memory the string copy will fail. The second statement is just an assignment statement. Just like diam = 3.14159265358979323846 * radius; changes the value diam, the statement string2="Tony"; changes the value of string2.
 
regarding to jtbell:
when you say " The first is initialization of the struct."

i am building a new type called human.

"The specified data is placed in the struct when the memory is allocated before the program begins execution. "

the data is not placed in the struct
its placed in the variable TR

"The second is an attempt to assign values to the struct during program execution, after memory has been allocated. "

i can't see the problem
first we create an array of some data type we created
by doing that we have a memory space for this array
then we take one cell from this array(it consists of 2 variables) and put there our values

of course it should happen during the execution of the program

what the memory problem?
 
in this example
we create the same variable twice
but here on the second time we are creating robots[3] using ROBOT (which is the same type as robot type)

why is it possible??

we are creating the same variable twice
Code:
#include <stdio.h>

typedef struct robot ROBOT;

struct robot { 
  char *name; 
  int energy; 
};

int main() {
  int i;
  
  ROBOT robots[3];                              //1st time

  robots[0].name = "Lunar Lee";
  robots[0].energy = 50;
  robots[1].name = "Planetary Pete";
  robots[1].energy = 20;
  robots[2].name = "Martian Matt";
  robots[2].energy = 30;

  
  ROBOT robots[3] = { {"Lunar Lee", 50},{"Planetary Pete", 20},{"Martian Matt", 30} }; //2nd time
 
thanks i got the idea
 
Back
Top