访问内部结构阵列的方向 [英] accessing the array inside array of structures

查看:124
本文介绍了访问内部结构阵列的方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个结构如下

 一个结构
{
    字符* AP;
    字符*输入[10];
    INTê;
};

然后我创造了这个结构等的阵列结构列表[100];

现在我想,并祝数组输入[10]和我使用的语法来访问的第一个位置:

  IP = 0;
    INP = 0;
    列表[IP] .inputs [INP]

但我刚开的错误错误C2107:非法索引,间接不得入内的编译code

请建议如何访问内部结构阵列的方向位置。

 问候
普里亚


解决方案

在这里,您使用的字符指针数组中的结构。所以,你最初由100创建列表中,您结构分配内存我认为你不适合您,字符指针数组创建内存。你必须为每一个字符指针造成内存。因此,我建议例如code。

 的#include<&stdio.h中GT;一个结构
{
字符* AP;
字符*输入[10];
INTê;
};诠释的main()
{
    INT IP = 0;
    INT INP = 0;
    结构列表[100];
    列表[IP] .inputs [INP] =(字符*)malloc的(25);
    scanf函数(%S,列表[IP] .inputs [INP]); //或其它复印功能来填充字符串
    输出(输出%S,列表[IP] .inputs [INP]);
}

I have a structure as follows

struct a
{
    char* ap;
    char* inputs[10];
    int e;
};

then I have created an array of this structure like struct a list [100];

now i want to fille the array inputs[10] and for that I am using the syntax to access the first location :

    ip=0;
    inp=0;
    list[ip].inputs[inp]

but I am gettin the error "error C2107: illegal index, indirection not allowed" on compiling the code

please suggest how to access the array location inside array of structure.

regards
priya 

解决方案

Here you use array of character pointer in your structure. So Initially you allocate memory for you structure by creation list of 100. I think you didn't create memory for you array of character pointer. You have to create memory for each of character pointer. So I suggest example code.

#include <stdio.h>

struct a
{
char* ap;
char* inputs[10];
int e;
};

int main()
{
    int ip=0;
    int inp=0;
    struct a list[100];
    list[ip].inputs[inp]= (char*)malloc(25);
    scanf("%s",list[ip].inputs[inp]);//Or other copy function to fill string
    printf("output %s",list[ip].inputs[inp]);
}

这篇关于访问内部结构阵列的方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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