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
Click For Summary

Discussion Overview

The discussion revolves around the differences in struct initialization and assignment in C, particularly focusing on why a specific data input method fails when dealing with struct variables in an array. Participants explore the implications of memory allocation and string handling in the context of user-defined types.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • One participant notes that the syntax used for initializing a struct works only at the time of declaration, while assignment after memory allocation does not work in the same way.
  • Another participant raises a concern about memory allocation for string variables, questioning whether constant strings are automatically assigned memory and the implications for using functions like strcpy.
  • A participant clarifies the difference between assigning a string to a pointer and copying a string into allocated memory, emphasizing the potential issues if memory is not properly allocated.
  • One participant challenges the explanation of struct initialization, arguing that the data is placed in the variable itself rather than the struct, and questions the nature of memory allocation during program execution.
  • Another participant discusses the apparent redundancy of defining the same struct type twice, questioning why this is allowed in the context of typedef and struct definitions.

Areas of Agreement / Disagreement

Participants express differing views on the nature of struct initialization versus assignment, as well as the handling of memory for string variables. There is no consensus on the underlying reasons for the observed behaviors in C programming.

Contextual Notes

Participants highlight limitations in understanding memory allocation and the behavior of string pointers, particularly in the context of struct usage and assignment in C. Some assumptions about memory management and string handling remain unresolved.

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
 

Similar threads

Replies
7
Views
2K
  • · Replies 10 ·
Replies
10
Views
7K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 6 ·
Replies
6
Views
6K
Replies
6
Views
3K
Replies
1
Views
14K
  • · Replies 13 ·
Replies
13
Views
21K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K