2013-10-29 70 views
0

的数组我有以下代码写入结构

#include <stdio.h> 
#include <stdlib.h> 
#include <stdint.h> 

typedef struct Example 
{ 
    uint16_t a; 
    uint16_t b; 
} ExampleStruct; 

void derp(struct Example * bar[], uint8_t i) 
{ 
    uint8_t c; 
    for(c = 0; c < i; ++c) 
    { 
     bar[c]->a = 1; 
     bar[c]->b = 2; 
    } 
} 

int main() 
{ 
    struct Example * foo; 
    uint8_t i = 3; 
    foo = malloc(i*sizeof(ExampleStruct)); 
    derp(&foo, i); 
    free(foo); 
    return 0; 
} 

我得到段错误和所有调试告诉我,代码停止工作,由于

bar[c]->a = 1; 

我试图重新安排到这一切的以下

(*bar)[c]->a = 1; 
(*bar[c])->a = 1; 
bar[c].a = 1; 
(*bar)[c].a = 1; 

并没有成功。我究竟做错了什么?我不明白为什么这是失败的,我不明白为什么bar [0],bar [1]和bar [2]的地址相距太远,每个地址只需要2个字节。

+3

它更改为'结构示例* bar'和'DERP(FOO,i)的'和'bar [c] .a'。就这样。你为什么要传递指针的地址?你不需要改变指针。只有数组中的元素。 – Elazar

+0

除了被调用的函数(我认为你也写了)之外,是否有某些特定的原因需要它传递foo by-address *?如果是这样,那么请按照Acme的回答建议。如果没有,那么按照Elazar的评论建议。没有这个或那个,你会调用未定义的行为。 – WhozCraig

+0

因为我想在主 –

回答

0

一些变化需要的,因为你试图传递对象的数组:

#include <stdio.h> 
#include <stdlib.h> 
#include <stdint.h> 
#include <inttypes.h> 
typedef struct Example 
{ 
    uint16_t a; 
    uint16_t b; 
} ExampleStruct; 

void derp(struct Example * bar[], uint8_t i) 
{ 
    uint8_t c; 
    for(c = 0; c < i; ++c) 
    { 
     bar[c]->a = 1; 
     bar[c]->b = 2; 
    } 
} 

int main() 
{ 
    struct Example * foo[3]; 
    uint8_t i = 3, c; 
    for(i = 0; i < 3; i++) 
    foo[i] = malloc(sizeof(ExampleStruct)); 
    derp(foo, i); 
    for(c = 0; c < i; ++c) 
    { 
     printf("\n%" PRIu16 " %" PRIu16 ,foo[c]->a,foo[c]->b); 
    } 
    for(i = 0; i < 3; i++) 
    free(foo[i]); 
    return 0; 
} 

struct Example * foo;可以保持单个指针struct Example类型的对象。而struct Example * bar[]可以容纳指向struct Example类型对象的指针数组。

在您的原始程序中,当c大于0时,将会划分错误,因为您没有为struct Example类型的对象分配任何指针。

bar[c]->a = 1; 
bar[c]->b = 2; 

对于静态对象:

#include <stdio.h> 
#include <stdlib.h> 
#include <stdint.h> 
#include <inttypes.h> 
typedef struct Example 
{ 
    uint16_t a; 
    uint16_t b; 
} ExampleStruct; 

void derp(struct Example bar[], uint8_t i) 
{ 
    uint8_t c; 
    for(c = 0; c < i; ++c) 
    { 
     bar[c].a = 1; 
     bar[c].b = 2; 
    } 
} 

int main() 
{ 
    struct Example foo[3]; 
    uint8_t i = 3, c; 
    derp(foo, i); 
    for(c = 0; c < i; ++c) 
    { 
     printf("\n%" PRIu16 " %" PRIu16 ,foo[c].a,foo[c].b); //accessing in main 
    } 
    return 0; 
} 
+0

为什么downvote?这太疯狂了。 – Sadique

+1

我认为你的建议无缘无故地让事情变得复杂。 OP想要使用一系列结构。指针指针是问题的一部分,而不是解决方案的一部分。 – Elazar

+0

那么为什么当这个答案没有问题的时候就应该低估了答案。这一切都取决于我们如何理解这个问题,我想给出一个解决方案来修正OP的代码而没有太多的改变。问题是'写入一个结构数组' - 这也可以用指针来完成。 – Sadique

0

没有必要通过&foo。保持简单:

// In a function declaration, it's (almost) always a pointer, not an array. 
// "struct Example bar[]" means *exactly* the same thing in this context. 
void init(struct Example * bar, int n) { 
    int i; 
    for (i = 0; i < n; ++i) { 
     bar[i].a = 1; 
     bar[i].b = 2; 
    } 
} 

int main() { 
    int n = 3; 
    struct Example * foo = malloc(n*sizeof(struct Example)); 
    init(foo, n); // passes the address of the array - &a[0] - to init 
    printf("The second element is {%u, %u}\n", foo[1].a, foo[1].b); 
    free(foo); 
    return 0; 
} 

输出:

第二个元素是{1,2}

+1

啊!现在我明白你的观点了!废话!对于误会感到抱歉。 – Sadique