指向结构内部结构的指针给我提供了从中获取信息的问题。 [英] A pointer to a struct inside a struct is giving me problems to get information from.

查看:72
本文介绍了指向结构内部结构的指针给我提供了从中获取信息的问题。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑下一个代码:



Consider the next code:

#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);
}





我在NetBeans上运行(我猜是gcc编译器)而且我不知道怎么写在使用gets和printf函数时,使用AddGymnast函数正常工作的正确语法。

我甚至无法将任何值分配给结构名称中的memeber名称或国家/地区,以这种方式从结构体操运动员:(*(*体操运动员).cred).country)=美国;

我也不能对阵列做出strcpy。



注意:我正在使用(我有限)使用C语法。



有谁知道如何获得它work?



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?

推荐答案

使用AddGymnast的参数列表中的数组语法,你会让自己变得困难。尝试

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

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



并在main中使用:


And use in main:

AddGymnast (&g[0], &c[0]);



正如提示:在有限大小的缓冲区上使用get可能会产生缓冲区溢出。使用一个更安全的版本,让你指定缓冲区大小。


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.


我修复了下面代码中的语法错误

I fixed the syntax error in the code below
#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);
}





请注意:包含凭证 struct到 Gymnast 一,即:



Please note: it would be better to include the Credentials struct into the Gymnast one, namely:

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


需要注意几点,以便您可以解决。

如果你坚持使用C,虽然我不知道你为什么会这么做,然后将数组作为指针的指针传递。



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 );





当然,这仍然无效,因为你没有将任何凭证结构链接到任何体操运动员的债牌成员。



.cred永远不会被设置,因此它将是随机的,代码将崩溃或更糟。您需要决定如何解决这个问题。通过将两个数组链接在一起或使用malloc为每个Gymnast分配一个Credentials结构。



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.


这篇关于指向结构内部结构的指针给我提供了从中获取信息的问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆