0

I've been reading up on pointers and arrays in C in an effort to learn how to implement something VLA-ish as a member of a struct. (Specifically, I need an array or array-like object with length different between instances of the struct but defined at compile-time for any particular struct instance.)

It seems to me as though something like this:

typedef struct example{
    unsigned char length;
    char *data;
}example;

int buildExample(example e, unsigned char l, char * const d){
    e.length = l;
    e.data = d;
    return 0;
    //not safe I know, but that fact isn't relevant to the question.
}


main(){
    example e;
    unsigend char a = 5;
    char b[] = {1, 2, 3, 4, 5};
    buildExample(e, a, b);
    int i = 0;
    while(i < e.length){
        printf("%d  %d\n", i, e.b[i]);
        i++;
    }
    printf("%d  %d\n", i, e.b[i]);
}

should result in something like this output:

0  1
1  2
2  3
3  4
4  5
5  some unknown value or a segfault, not sure which

and the various pointers and memory cells and such should go something like this:

before call to buildExample:

example e.data                 b               address 0, 1, 2...
|null pointer|         |ptr to address 0|      |  1  |  2  |  3  |  4  |  5  |

after call to buildExample:

example e.data                  address 0, 1, 2...
|ptr to address 0|              |  1  |  2  |  3  |  4  |  5  |

But instead I just get a segfault. If I swap out e.data for b, I get the 'unknown value' outcome (32765, 32766 or 32767, for whatever reason), and if I swap out e.data for a new int* c, defined and set equal to b in main, I get the same result as b, which implies to me that e.data is not in fact being set to b by buildExample.

Why not?

2
  • 1
    In addition to cdont's answer, this will still be run-time, not compile time as you say you want to achieve. Commented Feb 6, 2016 at 2:04
  • Thank you for the information! My comment on compile time was to indicate that the number was available at compile time, more than that I wanted it to be set at compile time, and so compile time solutions would work for me. Commented Feb 6, 2016 at 4:10

2 Answers 2

2

When calling buildExample() you're actually passing a copy of the e struct created in main(), so any changes will be lost when the function returns. Use a pointer instead.

int buildExample(example *e, unsigned char l, char * const d){
    e->length = l;
    e->data = d;
    return 0;
    //not safe I know, but that fact isn't relevant to the question.
}

And when calling:

buildExample(&e, a, b);

However you have other errors such as unsigend char a = 5; (should be unsigned not "unsigend") and trying to access an element named b (should be data).

buildExample(&e, a, b);
while(i < e.length){
    printf("%d  %d\n", i, e.data[i]);
    i++;
}
printf("%d  %d\n", i, e.data[i]);

Hope it helps!

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! That was a very important piece of information! The errors weren't actually in my code, I put them in by accident when copying over and simplifying it, so that's my error.
0

This posted code presents a few problems.

1) the return type from main() is always int not a blank

2) the function: buildExample() modifies the e parameter (on the stack) but does not have any effect on the e variable back in the caller Should pass a pointer to the struct and change the assignments to e->length and e->data

3) use unsigned not unsigend`

4) the use of meaningless variable and parameter names makes the code unnecessarily difficult to understand

5) in the calls to printf() the example struct has no member b

Strongly suggest actually compiling the posted code before posting a question.

typedef struct example
{
    unsigned char dataCount;
    char *pData;
} example;

void buildExample(example *pExample, unsigned char dataCount, char * const pData)
{
    pExample->dataCount = dataCount;
    pExample->pData = pData;
}


int main( void )
{
    example myExample;
    unsigned char dataCount = 5;
    char data[] = {1, 2, 3, 4, 5};

    buildExample(&myExample, dataCount, data);

    for( int i=0; i<myExample.count; i++)
    {
        printf("%d  %d\n", i, myExample.pData[i]);
    }
    printf("%d  %d\n", i, myExample.pData[i]);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.