Question

I hate posting this because there are so many of these, but none of them seem to address what I am seeing. The normal issues (undeclared functions, unintentional casts, misunderstanding of basic pointers) don't seem apply here. This is the stripped down version of my code:

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

extern void* malloc ( size_t size );

typedef struct {
    size_t      size;
    uint8_t*    buffer, curr, next;
} buffer_t;

void init( buffer_t* b, int size ) {
    b->size   = (size_t) size;
    b->buffer = (uint8_t*) malloc( sizeof(uint8_t) * b->size + 1 );
    b->curr   = (uint8_t*) b->buffer; // warning: assignment makes integer from pointer without a cast [enabled by default]
    b->next   = (uint8_t*) b->buffer; // warning: assignment makes integer from pointer without a cast [enabled by default]
}

int main ( int argc, char* argv[] ) {
    buffer_t buf;

    init( &buf, 16 );

    return 0;
}

This fails without the casts, but putting them in makes it even more obvious.

I'm compiling on WinXP (yeah, yeah, yeah) under MinGW/MSYS with gcc 4.7.2. using the following command:

gcc -std=c99 -Wall -o testing test.c

Any help?

Was it helpful?

Solution

uint8_t*    buffer, curr, next;

The way you wrote it, buffer is a pointer and curr and next are mere uint8_ts. You probably meant:

uint8_t    *buffer, *curr, *next;

Even better (less prone to errors) would be to have each field on its own line.

OTHER TIPS

You have declared curr and next as uint8_t (and not a pointer to uint8_t) inside the structure declaration. Try this instead. uint8_t *buffer,*curr,*next;

This is a good question. Accepting your structure at face value and assuming curr and next should be pointers to other occurrences of the same structure (will also require code modification per other answers), then init should be coded as:

void init( buffer_t* b, int size ) {
    b->size   = (size_t) size;
    b->buffer = (uint8_t*) malloc( sizeof(uint8_t) * b->size + 1 );
    b->curr   = b;     // a self-pointer to this buffer_t
    b->next   = NULL;  // pointer to next buffer_t structure
}

b->buffer points to storage allocated on the heap, while the buffer_t structure is part of a linked list used to manage many different buffers.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top