Ç不兼容的类型中分配,使用指针的问题? [英] c incompatible types in assignment, problem with pointers?

查看:124
本文介绍了Ç不兼容的类型中分配,使用指针的问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我用C工作,我有一个关于分配问题的指针。

Hi I'm working with C and I have a question about assigning pointers.

struct foo
{
   int _bar;
   char * _car[SOME_NUMBER]; // this is meant to be an array of char * so that it can hold pointers to names of cars
}

int foofunc (void * arg)
{
   int bar;
   char * car[SOME_NUMBER];

   struct foo * thing = (struct foo *) arg;

   bar = thing->_bar; // this works fine
   car = thing->_car; // this gives compiler errors of incompatible types in assignment
}

汽车_car具有相同的声明,那么为什么我会收到关于不兼容类型的错误?我的猜测是,它有话跟他们是指针(因为它们是指向char *数组,对吧?),但我不明白为什么这是一个问题。

car and _car have same declaration so why am I getting an error about incompatible types? My guess is that it has something to do with them being pointers (because they are pointers to arrays of char *, right?) but I don't see why that is a problem.

当我宣布的char *车; 而不是中的char *汽车[MAXINT]; 它编译的罚款。但我不明白如何当我需要使用索引,这将是非常恼人稍后访问信息访问某些信息,这将是对我有用更高版本。事实上,我甚至不知道如果我要了解正确的方式,也许有存储一串字符串,而不是使用字符数组的一种更好的方式*?

when i declared char * car; instead of char * car[MAXINT]; it compiles fine. but I don't see how that would be useful to me later when I need to access certain info using index, it would be very annoying to access that info later. in fact, I'm not even sure if I am going about the right way, maybe there is a better way to store a bunch of strings instead of using array of char *?

编辑:我不是故意用INT_MAX(INT的最大值),它只是一些其他的INT,也就是约20

I didn't mean to use INT_MAX (maximum value of int), it's just some other int, which is about 20.

推荐答案

您正在创建规模MAXINT的新数组。我想你想创建一个指向大小MAXINT的数组。

You are creating a new array of size MAXINT. I think you want to create a pointer to an array of size MAXINT.

创建一个指向字符数组*的:

以下是规模最大整数数组为char *元素:

The following is an array of size MAXINT to char* elements:

char * car[MAXINT]; 

下面是一个指向:大小MAXINT的数组为char *元素:

The following is a pointer to: an array of size MAXINT to char* elements:

char* (*car)[MAXINT];

以下是你如何设置一个指向:大小MAXINT的数组为char *元素:

the following is how you set a pointer to: an array of size MAXINT to char* elements:

char* (*car)[MAXINT];
car = &arg->_car;


在这个问题其他语法错误:


  • 您需要您的结构定义后有一个分号。

  • 您应该使用富* 不是。因此它应该是:搜索
    结构美孚*的东西=(结构美孚*)ARG;

  • 您应该使用的事情不是 ARG :结果
    酒吧=东西─> _bar; 结果
    汽车=东西─> _car;

  • You need to have a semicolon after your struct definition.
  • You should be using foo* not foo. So it should be:
    struct foo* thing = (struct foo *) arg;
  • You should be using thing not arg:
    bar = thing->_bar;
    car = thing->_car;

这篇关于Ç不兼容的类型中分配,使用指针的问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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