pointer from integer without cast

This is a discussion on pointer from integer without cast within the C Programming forums, part of the General Programming Boards category; sup people, i'm making a medium project ( around 2k lines of code), and everything seems to work fine, except ...

  1. #1
    Registered User DeliriumCordia's Avatar
    Join Date
    Mar 2012
    Posts
    59

    pointer from integer without cast

    sup people, i'm making a medium project ( around 2k lines of code), and everything seems to work fine, except some warning in the compile (gcc) that tell me i'm making pointers from integer without cast.

    i really can't understand why, i'll try to post some snippet to allow you to see if something seems to be faulty.

    error is raised on the initialization of a buffer, a struct i declared that is of type buffer_t.

    buffer is a pointer to a buffer_t struct, so is of type buffer_t*. the function to initialize this buffer is buffer_init, that takes as parameter an unsigned int and returns a buffer_t*.

    this line of code raises the warning "makes pointer from integer without cast".

    Code:
     buffer = buffer_init(1)
    so i'm declaring the buffer_t* buffer as the result of buffer_init, that returns the pointer to the struct buffer_t.

    here it is the buffer_init method:

    Code:
    buffer_t* buffer_init(unsigned int maxsize) {
        
    
        int bufferid = shmget(ftok("buffer.c", 'B'), sizeof(buffer_t), IPC_CREAT | 0666);
        if(bufferid == -1) {
            perror("error in allocation of buffer shared memory. \n"); exit(-13);
        }
    
        int new_message_id = shmget(ftok("buffer.c", 'A'), maxsize*sizeof(msg_t), IPC_CREAT | 0666);
        if(new_message_id == -1) {
            perror("error in allocation of msg_t array. \n"); exit(-14);
        }
    
        int text_id=shmget(ftok("buffer.c", 'T'), maxsize * TEXT_SIZE, IPC_CREAT|0666);
        if(text_id==-1)
            perror("shmget dell'array text fallito");
        
    
        int sem_id = semget(ftok("buffer.c", 'S'), 4, IPC_CREAT | 0666);
    
    
        buffer_t* new_buffer = (buffer_t*) shmat(bufferid,NULL, 0);
        msg_t* new_message = (msg_t*) shmat(new_message_id, NULL, 0);
    
        if((sem_initialize(sem_id, maxsize))== -1) {
            perror("semctl() fails to inizialize semaphores."); exit(-3);
        }
    
        new_buffer->text_array_id = text_id;
    
        new_buffer->D=0;
        new_buffer->T=0;
        new_buffer->dimension = maxsize;
        new_buffer->msgbuffer = new_message;
        new_buffer->id_sem = sem_id;
        new_buffer->shid_buffer = bufferid;
        new_buffer->shid_msg = new_message_id;
        new_buffer->text_array=(char*)shmat(text_id,NULL,0);
    
        return new_buffer;
    }
    lot of the stuff is regarding ipc setup for process communication.

    any hint?

    PS: ive seen including <string.h> is necessary, i included it in all the source files with no luck.

  2. #2
    Registered User Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    650
    Since the typedef is already a pointer, should you really be declaring buffer_t* buffer (i.e. a struct buffer_t **)? I would have thought buffer_t buffer would be want you want.

    PS This is why, in general but not all cases, I hate typedefs... especially of this type.

  3. #3
    Registered User DeliriumCordia's Avatar
    Join Date
    Mar 2012
    Posts
    59
    hmm, maybe i explained myself in a wrong way (or maybe i didn't understand well what you suggested to me), but this is the def of the buffer_t struct:

    Code:
     
    typedef struct buffer {
         msg_t* msgbuffer;
         int dimension;
         int T;
         int D;
         int id_sem;
         int shid_buffer;
         int shid_msg;
         char* text_array;
         int text_array_id;
     } buffer_t;
    so new_buffer, declared as buffer_t* is returned, (so i return a buffer_t*), and in the "main" i call buffer_init on a buffer_t* typed var. is it wrong?
    Last edited by DeliriumCordia; 12-27-2013 at 06:06 AM.

  4. #4
    Registered User Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    650
    Does buffer_t* buffer_init(unsigned int maxsize) have a prototype in scope?

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    2,697
    It would help if you also posted your error messages, exactly as they appear in your development environment.


    Jim

  6. #6
    Registered User DeliriumCordia's Avatar
    Join Date
    Mar 2012
    Posts
    59
    Quote Originally Posted by Hodor View Post
    Does buffer_t* buffer_init(unsigned int maxsize) have a prototype in scope?
    that was the issue, thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Making integer from pointer without a cast
    By Sorinx in forum C Programming
    Replies: 8
    Last Post: 12-16-2013, 12:38 AM
  2. Pointer to integer without a cast
    By javaeyes in forum C Programming
    Replies: 9
    Last Post: 03-11-2012, 10:01 AM
  3. makes pointer from integer without a cast
    By nyquil in forum C Programming
    Replies: 5
    Last Post: 05-06-2010, 06:40 AM
  4. integer to pointer without cast
    By eidgeare in forum C Programming
    Replies: 3
    Last Post: 12-08-2009, 02:14 PM
  5. Pointer from integer without cast
    By STDSkillz in forum C Programming
    Replies: 2
    Last Post: 10-22-2007, 09:39 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21