Thread: global structure pointers

  1. #1
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214

    global structure pointers

    hi!

    assume we have a function to print out a header of a specific protocol like ip.
    i have my prototype function declared
    Code:
    void print_ip(struct iphdr *ip);
    in main i initialise a pointer to the header.
    Code:
    struct iphdr *ip;
    
    ip = (struct iphdr *) (DATA_BUFFER + ipheadersize + ethernetheadersize);
    my problem is that i cant access the pointer in every function itself because its declared in main.

    when i write all that as globals, gcc comes up with errors like: initialization makes integer from pointer without a cast.

    why is this happening? and why isnt this happening when i initialize the structure pointers in main?

    thank you

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Post a bit more code, showing how you're trying to do it.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214
    i want to use my code like that:

    Code:
    /*globals*/
    
    
    #define BUFFER_MAX 2048
    char DATA_BUFFER[BUFFER_MAX];
    
    
    struct headers
    {
        int i_size;
        int e_size;
    } headersizes;
    
    struct headersizes = {sizeof(iphdr), sizeof(ethhdr)};
    
    struct iphdr *ip;
    ip = (struct iphdr *) (DATA_BUFFER + headersizes.e_size + headersizes.i_size);
    
    //protos
    
    void print_ip(struct iphdr *ip);
    ...
    ...
    int main(int argc, char **argv)
    {
        ...
        ...
        
        read(sockfd, DATA_BUFFER, sizeof(DATA_BUFFER)-1);
        //i read everything into the DATA_BUFFER var
    
        //now i want to call a function to print out the ip header
    
        print_ip(ip);
        return 0;
    }
    
    void print_ip(struct iphdr *ip)
    {
        printf("length: %d\n", ip->lenght);
        printf("......the common ip header infos\n");
        ...
        ...
    }
    is there anything wrong with this initialization/assignment of ip?

    thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorted Linked List...What am i doing wrong!
    By S15_88 in forum C Programming
    Replies: 9
    Last Post: 03-20-2008, 03:29 PM
  2. Use global variables or pointers?
    By RealityFusion in forum C++ Programming
    Replies: 5
    Last Post: 09-22-2005, 08:47 PM
  3. Global Structure
    By computerfreaks in forum C Programming
    Replies: 8
    Last Post: 02-18-2005, 01:59 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. How To Detect Pointers In Structure
    By GaPe in forum C Programming
    Replies: 8
    Last Post: 07-31-2003, 04:12 PM