Click here to Skip to main content
15,884,425 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Consider the next code:

C++
#include <stdio.h>
#include <stdlib.h>

struct SCredentials
{
    char name[30];
    char country[20];
};
typedef struct SCredentials Credentials;

struct SGymnast{
    char sport[20];
    int nMedals[3];
    Credentials * cred;
};
typedef struct SGymnast Gymnast;


void AddGymnast(Gymnast (*gymnast)[],Credentials (*credentials)[],int pos)
{
puts("Pais: "); gets((*gymnast)[pos].cred[pos].country); puts("\n");
    printf("Pais es: %s",(*gymnast)[pos].cred[pos].country);
}


int main(int argc, char** argv) {
    int r;
    Gymnast g[20];
    Credentials c[20];
    
    AddGymnast(g,c,0);
    
    return (EXIT_SUCCESS);
}


I am running on NetBeans (I guess is gcc compiler) and I don't know how to write the right syntax to get the function "AddGymnast" to work properly when using the "gets" and "printf" function.
I cannot even assign any value to the memeber name nor country inside the struct Credentials, from the struct Gymnast in this manner: (*(*gymnast).cred).country) = "USA";
Neither can I make a "strcpy" to the array.

Note: I am using (and I am limited) to using C syntax.

Does anybody know how to get it work?
Posted

With the array syntax in the argument list of AddGymnast you are making things hard onto yourself. Try
C++
void AddGymnast (Gymnast *gymnast, Credentials *credentials)
{
    puts ("Pais: ");
    gymnast->cred = credentials;
    gets (credentials->country);
    puts ("\n");

    printf ("Pais es: %s", gymnast->cred->country);
}

And use in main:
C++
AddGymnast (&g[0], &c[0]);

Just as a hint: Using gets on a limited size buffer has a risk of producing a buffer overrun. Use one of the safer versions that let you specify the buffer size.
 
Share this answer
 
I fixed the syntax error in the code below
C
#include <stdio.h>
#include <stdlib.h>

struct SCredentials
{
    char name[30];
    char country[20];
};
typedef struct SCredentials Credentials;

struct SGymnast{
    char sport[20];
    int nMedals[3];
    Credentials * cred;
};
typedef struct SGymnast Gymnast;


void AddGymnast(Gymnast gymnast[],Credentials credentials[],int pos)
{
    gymnast[pos].cred = &credentials[pos];
    puts("Pais: "); gets(gymnast[pos].cred->country); puts("\n");
  printf("Pais es: %s",gymnast[pos].cred->country);
}


int main(int argc, char** argv) {
    int r;
    Gymnast g[20];
        Credentials c[20];


    AddGymnast(g,c,0);

    return (EXIT_SUCCESS);
}


Please note: it would be better to include the Credentials struct into the Gymnast one, namely:
C
struct SGymnast{
    char sport[20];
    int nMedals[3];
    Credentials cred;
};
 
Share this answer
 
Comments
unscathed18 11-Apr-13 10:24am    
Yeah, I did, but I had a problem with the gcc compiler: It seems that the SGymnast struct crushes the inner struct, Credentials, and I guess is due to some bug.
When I do swap the order of declaration of variables within the SGymnast struct, it seems that everythings go messy. For instance, the sport string prints onscreen garbage info.

On the other hand, if I put it Credentials at the end such a problem doesnt occur.

So for this reason, I opted for making the "cred" a pointer to a Credentials struct instead the struct itself.
nv3 11-Apr-13 10:45am    
I would say that it is highly unlikely that you are observing a compiler bug here. Your description sounds more like you are exceeding the buffer space of one of the character buffers and thereby overwriting the following structure members. You might want to try again and simply make those fields in credentials larger to prove or falsify this theory.
CPallini 11-Apr-13 12:02pm    
I completely agree.
unscathed18 11-Apr-13 12:56pm    
I also thought such a thing but then I realised that I wasn't exceeding the buffer length. There was too much space left for happening.
nv3 11-Apr-13 13:16pm    
If you want, you can post the code in the original sort order, we can take a look at it and help you find the real cause of the problem. This is usually done via the green Improve Question button, then insert a mark like [Edit: amendment to the original question] and your code.
Several things to note so that you can work it out.
If you're sticking to C although I have no idea why you would, then pass arrays as pointers to pointers.

void AddGymnast( Gymnast** gymnast, Credentials** credentials, int pos )
...


Instead of trying to read data directly from the user into these arrays read it into some temporary variables first. This will make it easier to see what's going on and will help you later when you want to validate the user input beore allowing it to just write all over memory.

char TempBuffer[ 20 ];
gets( TempBuffer );

memcpy( (*gymnast)[pos].cred[pos].country, TempBuffer, 19 );

printf( "Pais es: %s",(*gymnast)[pos].cred[pos].country );


Of course this will still not work because you have not linked any Credentials structure to the cred member of any of the Gymnasts.

.cred is never set so it will be random and the code will crash or worse. You need to decide how you want to solve this. Either by linking the two arrays together or by using malloc to allocate a Credentials structure for each Gymnast.
 
Share this answer
 
Comments
unscathed18 11-Apr-13 10:35am    
Good. One more question: When you declare a 20 characters string (i.e. buffer),
it means that will hold 20 + the '\0'? Or it means 19 characters + '\0'?
nv3 11-Apr-13 10:46am    
The latter one: 19 + '\0'.
Thanks all for your answers. Not assigning any value to the pointer was in fact a mistake of mine with the rush I had to post this on the forum.
 
Share this answer
 
Comments
nv3 11-Apr-13 10:48am    
No problem. You can thank the folks that have helped you locating the trouble by accepting their answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900