Error saving data in a struct

4

This is how I declare my struct, but at the time of saving it saves twice the data int semaforo; .

struct mystruct
{
  char arr[MAXBUF];  
}es;

struct mystruct *entrada;

char g[20]={"11111111111111111118"};

So I save it and print it:

for (int i = 0; i < 2; ++i)
{
    strcpy ( entrada[i].arr, g);
}

{
    printf("%s\n",entrada[0].arr);
}

... and I get it:

1111111111111111111811111111111111111118
11111111111111111118

... and it should come out:

11111111111111111118
11111111111111111118

What am I doing wrong?

    
asked by jon 11.11.2016 в 03:12
source

2 answers

1

There are several problems with your code.

The first is that you need to leave a free character in your buffer for the NULL (\ 0) that the compiler places at the end of each string. Therefore in your line:

char g[20]={"11111111111111111118"};

You should enlarge the buffer to 21 or write a character less.

As a second pronlema, you are confusing pointers with arrays.

Let's start at the beginning:

struct mystruct { char arr[MAXBUF];
}es;

There what you are doing is to define your structure (type of new data). It is important to understand that this definition does not reserve space in memory, since it does not declare any variable. (Let me say that it is very strange to declare a structure to just place an array inside it, but ignore that for now)

Then you do the following:

struct mystruct *entrada;

Here if you are declaring a variable, but what you are declaring is a pointer to a structure, that is, it will only allow you to store memory addresses of other structures, and what I see in your code is not what you want to do .

What you need is an array of structures and you should declare it as follows:

struct mystruct entrada[2];

That way you declare an array of structures that will allow you to store two of them.

To print all the data contained in the structure, you would also have to perform a for as you did to load it:

for (int i = 0; i < 2; ++i)
{
    printf("%s\n",entrada[i].arr);
}

Make those changes and it should work

    
answered by 11.11.2016 в 03:40
0

You have multiple failures; the mysterious thing is that you do not get a general protection error or a SIGSEGV.

struct mystruct *entrada;

There you are creating a pointer to your structure, which initially has an undetermined value and we do not know where it points. Your code does not show how you initialize that pointer.

strcpy ( entrada[i].arr, g);

When i == 0 , you are copying the content of a string on the first element of a supposed array of your structures , not on the first element of the array arr of your structure.

When i == 1 , you are copying the contents of a string over the second element of an assumed array of your structures , not over the second element of the array arr of your structure.

strcpy( destino, origen );

This function goes through the memory, from the position marked by origen , and is copying bytes to destino , sequentially until it finds a byte \ 0, that also is copied .

That means that when you do

char g[20]={ "11111111111111111118" };

for (int i = 0; i < 2; ++i) {
 strcpy ( entrada[i].arr, g);
}

Actually, you are copying 2 times the entire string g [20] , starting the second copy just after the data copied the first time strong>.

If your entrada is effectively an array, has happened that, due to its size and alignment, its memory spaces are just one behind the other . That means that the second call to strcpy() overwrites the byte \ 0 that the first call put at the end of entrada[0] . That overwrite causes printf() to see no separation between the strings, and show them together.

Although in C the pointers and arrays are equivalent, you are mixing them in a way, at least, curious .

    
answered by 11.11.2016 в 06:11