Printing Arrays from Structs

Posted by Carlll on Stack Overflow See other posts from Stack Overflow or by Carlll
Published on 2012-09-22T03:16:29Z Indexed on 2012/09/22 3:37 UTC
Read the original article Hit count: 363

Filed under:
|

I've been stumped for a few hours on an exercise where I must use functions to build up an array inside a struct and print it. In my current program, it compiles but crashes upon running.

#define LIM 10

typedef char letters[LIM];

typedef struct {
    int counter;
    letters words[LIM];
} foo;


int main(int argc, char **argv){
    foo apara;
    structtest(apara, LIM);
        print_struct(apara);

}

int structtest(foo *p, int limit){
    p->counter = 0;
    int i =0;
    for(i; i< limit ;i++){
        strcpy(p->words[p->counter], "x");
                //only filling arrays with 'x' as an example
        p->counter ++;
    }
    return;

I do believe it's due to my incorrect usage/combination of pointers. I've tried adjusting them, but either an 'incompatible types' error is produced, or the array is seemingly blank

}

void print_struct(foo p){
    printf(p.words);

}

I haven't made it successfully up to the print_struct stage, but I'm unsure whether p.words is the correct item to be calling. In the output, I would expect the function to return an array of x's. I apologize in advance if I've made some sort of grievous "I should already know this" C mistake. Thanks for your help.

© Stack Overflow or respective owner

Related posts about c

    Related posts about struct