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

  • Thread starter transgalactic
  • Start date
  • Tags
    Data Input
In summary, when i got a normal struct variable 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 objectthe same data input method doesn't workwhy is that?#include <stdio.h>typedef struct robot ROBOT;struct robot { char *name;
  • #1
transgalactic
1,395
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
  • #2
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.
 
  • #3
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:
  • #4
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.
 
  • #5
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?
 
  • #6
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
 
  • #7
thanks i got the idea
 

1. What is a "Struct data input problem"?

A struct data input problem refers to a situation where there is a need to organize and input various types of data into a structured format for efficient processing and analysis. This can arise in various fields such as computer science, engineering, and data analysis.

2. What are the common challenges faced when dealing with struct data input problems?

The common challenges include identifying the appropriate data structure, understanding the relationships between different data elements, and ensuring the accuracy and completeness of the data. Additionally, converting data from different sources into a consistent format can also be a challenge.

3. How do scientists typically approach solving struct data input problems?

Scientists typically start by analyzing the data and identifying the key data elements and their relationships. Then, they choose an appropriate data structure, such as arrays, linked lists, or trees, to organize the data. Finally, they may use algorithms and programming techniques to efficiently input and manipulate the data.

4. Can automated tools be used to solve struct data input problems?

Yes, there are various automated tools and software available that can assist in solving struct data input problems. These tools can help with data organization, data cleaning and validation, and data transformation from one format to another.

5. How important is proper data input in scientific research?

Proper data input is crucial in scientific research as it forms the foundation for accurate and reliable analysis and conclusions. If the data is not properly input, it can lead to incorrect results and hinder the progress of research. Therefore, scientists must pay close attention to data input and ensure its accuracy and completeness.

Similar threads

  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
10
Views
6K
  • Programming and Computer Science
Replies
5
Views
3K
  • Programming and Computer Science
Replies
6
Views
5K
  • Astronomy and Astrophysics
Replies
4
Views
1K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
13
Views
19K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
Back
Top