Thread: pointint to an array from within a structure

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    8

    pointint to an array from within a structure

    alright i seem to have hit a brick wall with all the errors im getting on this one.

    what i want to do is point to my multidimensional array history in the structure and pass this around to various functions so that i can put things into the history array.

    i keep getting the compilation error "warning: assignment from incompatible pointer type"

    any help to solve this is great

    Code:
    struct clientconn
    {
    	int							sockfd;
    	int 							listenfd;
    	char 							*cliIP;
    	struct history_entry 	**history;
    };
    
    struct clientconn		*conn;
    struct history_entry history[100][10];
    
    conn = malloc(sizeof(struct clientconn));
    conn->sockfd = Accept(listenfd, cliaddr, &len);
    conn->listenfd = listenfd;
    conn->cliIP = inet_ntoa(cliaddr->sin_addr);
    conn->history = history;

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    A pointer to a pointer is not the same as a pointer to an array of arrays. Here is a FAQ on the topic. You might want to browse the whole index of the topic of Arrays and Pointers.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Location
    India
    Posts
    167
    Code:
    int p[10];
    int *x;
    x=p;
    By increasing number of *(star) before a pointer it appears you can increase the number of [](brackets) after an array like
    Code:
    int p[][];
    int **x;
    x=p
    But **x is a pointer to a pointer.
    To expain it further like you if you have to pass a single dim array to function you can do this
    Code:
    int p[];
    test(int *p);
    now suppose you have to pass two dim array,you cant pass array like this
    Code:
    int p[][];
    test(int **p);
    Last edited by cbastard; 10-21-2005 at 07:42 AM.
    Long time no C. I need to learn the language again.
    Help a man when he is in trouble and he will remember you when he is in trouble again.
    You learn in life when you lose.
    Complex problems have simple, easy to understand wrong answers.
    "A ship in the harbour is safe, but that's not what ships are built
    for"

  4. #4
    Registered User
    Join Date
    Jun 2004
    Posts
    8
    ok so i went backe and changed the structure to only point to the array with

    Code:
    stuct history_entry history[10][100];
    
    struct history_entry 	*historyptr;
    but how do i assign the historyptr to the array? i tried

    Code:
    conn->historyptr = history;
    
    as well as 
    
    conn->historyptr = &history;
    but both give me "warning: assignment from incompatible pointer type"

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well that's wrong too. I take it you didn't bother reading either of the links I provided.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Location
    India
    Posts
    167
    Code:
    struct clientconn
    {
    	int							sockfd;
    	int 							listenfd;
    	char 							*cliIP;
    	struct history_entry 	*history[];
    };
    may be something like this
    Long time no C. I need to learn the language again.
    Help a man when he is in trouble and he will remember you when he is in trouble again.
    You learn in life when you lose.
    Complex problems have simple, easy to understand wrong answers.
    "A ship in the harbour is safe, but that's not what ships are built
    for"

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No, that's wrong too. That's an array of pointers to struct history_entry. You too should go read the links I provided.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Location
    India
    Posts
    167
    Oh sorry i mean to say
    Code:
    struct clientconn
    {
    	int							sockfd;
    	int 							listenfd;
    	char 							*cliIP;
    	struct history_entry 	(*history)[];
    };
    my bad

    >>No, that's wrong too. That's an array of pointers to struct history_entry. You too should go read the links I provided.
    yeah sure i will.
    Long time no C. I need to learn the language again.
    Help a man when he is in trouble and he will remember you when he is in trouble again.
    You learn in life when you lose.
    Complex problems have simple, easy to understand wrong answers.
    "A ship in the harbour is safe, but that's not what ships are built
    for"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structure or Array?
    By epi_jimenez in forum C Programming
    Replies: 7
    Last Post: 04-01-2009, 02:45 PM
  2. linear search for structure (record) array
    By jereland in forum C Programming
    Replies: 3
    Last Post: 04-21-2004, 07:31 AM
  3. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  4. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM