Thread: Passing an array from within a Struct into a function...

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    9

    Passing an array from within a Struct into a function...

    Hi All,

    I've got a struct which has several integer arrays inside of it. I'm trying to pass this struct into a function but I'm getting an error which says that the variables are not a member or something.

    "error: request for '_src' in something not a structure or union"

    Code:
    struct temp_Packet{
        int src, dst, typ, prt;
        char data[51], buffer[51];
    }temp;
    //end of temp_Packet
    
    
    struct SPackets{
        int _src[50], _dst[50], _typ[50], _prt[50];
        char _data[5][51];
    }packets;
    //end of SPackets
    
    /********************
    -----------------------
    *********************/
    
    void set_values(struct SPackets *packets, struct temp_Packet temp){
        *packets._src[nPacket] = temp.src;
        *packets._dst[nPacket] = temp.dst;
        *packets._typ[nPacket] = temp.typ;
        *packets._prt[nPacket] = temp.prt;
        strcpy(*packets._data[nPacket], temp.data);
    
    
        printf("\nSrc: %i Dst: %i Typ: %i Prt: %i Data: %s\n",
               *_src[nPacket] ,*_dst[nPacket],*_typ[nPacket] ,*_prt[nPacket],*_data[nPacket]);
    }
    //end of SET_VALUES
    
    int new_packet(struct SPackets packets, int goagain, struct temp_Packet temp){
    
    
        goagain=nPacket;
    
    
        printf("\nSource Address (1-1024): ");
        scanf("%s", temp.src);
    
    
        printf("\nDestination Address (1-1024): ");
        scanf("%s", temp.dst);
    
    
        printf("\nType (0-10): ");
        scanf("%s", temp.typ);
    
    
        printf("\nData (0-50 characters): ");
        scanf("%s", temp.prt);
    
    
        set_values(&packets, temp);
        nPacket++;
    //removed some coding to make it simpler to read
    
    
    }
    //end of NEW_PACKET
    /********************
    -----------------------
    *********************/
    
    int main(){
    
    set_values(&packets, temp);
    
    }
    What I'm think the problem is that I haven't passed the struct through to the functions properly?

    If there's anymore information needed to fix this I'd be more than happy to try and give it, this is doing my head in!

    Thanks a lot in advance for any help that you can give!

    Shini

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You need some parentheses (in a lot of places). For example,
    Code:
    (*packets)._src[nPacket] = temp.src;
    However, it's usual to use the arrow operator in this case, since that's exactly what it's for.
    Code:
    packets->_src[nPacket] = temp.src;
    BTW, your temp struct is not initialized.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    9
    Ah right! Well I've changed it and it finally compiles!

    So something like this....

    Code:
    void set_values(struct SPackets *packets, struct temp_Packet temp){
        packets->_src[nPacket] = temp.src;
        packets->_dst[nPacket] = temp.dst;
        packets->_typ[nPacket] = temp.typ;
        packets->_prt[nPacket] = temp.prt;
        strcpy(packets->_data[nPacket], temp.data);
    
    
        printf("\nSrc: %i Dst: %i Typ: %i Prt: %i Data: %s\n",
               packets->_src[nPacket] , packets->_dst[nPacket], packets->_typ[nPacket] , packets->_prt[nPacket], packets->_data[nPacket]);
    }
    //end of SET_VALUES
    Thanks for that, a quick question is the function parameters still correct though?

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    I don't know. As I said, as it is so far you haven't assigned any values to temp in main, so set_values is just assigning garbage to packets. So you need to fix that. Also, you may want to pass temp as a pointer as well, and use the arrow operator with it too.

    Also, I don't see any npacket declaration.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    9
    Ah of course, sorry about that, I'd only put in the information regarding the passing of structs into a function because I didn't realise that I had another problem.

    I've put down the full code for new_packet() at the bottom it calls up set_values(). Also, I've included the coding for validate_string() and validate_datastring() which simply checks that the inputs are all integers. It's quite a stupid way of doing it but it seems to work right now. I think an alternative would be to simply check that the string that user inputs is between the ASCII values for numbers?

    Also, nPacket has been declared as a global variable, though I suspect it would be much neater to declare it within the struct SPacket.

    Code:
    int new_packet(struct SPackets *packets, int goagain, struct temp_Packet *temp){
        char repeat='Y';
        goagain=nPacket;
    
    
        printf("\nSource Address (1-1024): ");
        scanf("%s", temp->buffer);
        temp->src = validate_string(temp->buffer, temp->src);
        while((temp->src > 1024)||(temp->src < 1)){
            printf("\nInvalid data entry, please re-enter: ");
            scanf("%s", temp->buffer);
            temp->src = validate_string(temp->buffer, temp->src);
        }
    
    
        printf("\nDestination Address (1-1024): ");
        scanf("%s", temp->buffer);
        temp->dst = validate_string(temp->buffer, temp->dst);
        while((temp->dst > 1024)||(temp->dst < 1)){
            printf("\nInvalid data entry, please re-enter: ");
            scanf("%s", temp->buffer);
            temp->dst = validate_string(temp->buffer, temp->dst);
        }
    
    
        printf("\nType (0-10): ");
        scanf("%s", temp->buffer);
        temp->typ = validate_string(temp->buffer, temp->typ);
        while(temp->typ > 10){
            printf("\nInvalid data entry, please re-enter: ");
            scanf("%s", temp->buffer);
            temp->typ = validate_string(temp->buffer, temp->typ);
        }
    
    
        printf("\nPort (1-1024): ");
        scanf("%s", temp->buffer);
        temp->prt = validate_string(temp->buffer, temp->dst);
        while((temp->prt > 1024)||(temp->prt < 1)){
            printf("\nInvalid data entry, please re-enter: ");
            scanf("%s", temp->buffer);
            temp->prt = validate_string(temp->buffer, temp->prt);
        }
    
    
        printf("\nData (0-50 characters): ");
        scanf("%s", temp->data);
        while (validate_dataString(temp->data)==false){
            printf("\nInvalid data entry, please re-enter: ");
            scanf("%s", temp->data);
        }
    
    
        set_values(&packets, &temp);
        nPacket++;
    
    
        printf("\nPacket %i created.\n\nDo you wish to create another packet?\n: ", nPacket);
        scanf("%c", &repeat);
    
    
        if((repeat=='Y')||(repeat=='y')){
            return goagain++;
        } else {
            return goagain=51;
        }
    }
    //end of NEW_PACKET
    
    int validate_string(char *string, int x) /*validates that the entry consists only of ints */{
        int _strlength = strlen(string), i=0;
        for (i=0; i<_strlength; i++){
            //isdigit() not declared in C?
            if(!isdigit(string[i])){
                printf("\nInvalid entry, please re-enter: ");
                scanf("%s", string);
                validate_string(string, x);
            }
        }
        x = atoi(string);
        return x;
    } //end of VALIDATE_STRING()
    //end of VALIDATE STRING
    
    
    bool validate_dataString(char *string){
        int _strlength = strlen(string);
        unsigned int i=0;
        for (i=0; i<_strlength; i++){
            if(!isdigit(string[i])){
                return false;
            }
        }
        return true;
    }
    //end of VALIDATE_DATASTRING()

  6. #6
    Registered User
    Join Date
    May 2012
    Posts
    9
    UPDATE: Managed to get that particular section fixed, however I now have another issue with similar problems.
    UPDATE2: Changed the code so that its actually more like my code, apologies for being such an idiot.
    Changes that have been made to the code below: put in new_packets() and removed the global temp_Packet temp.

    Code:
    struct temp_Packet{
        int src, dst, typ, prt;
        char data[51], buffer[51];
    };
    //end of temp_Packet
    
    struct SPackets{
        int _src[50], _dst[50], _typ[50], _prt[50];
        char _data[51][51];
    }packets;
    //end of SPackets
    
    int new_packet(struct SPackets *packets, int goagain);
    void set_values(struct SPackets *packets, struct temp_Packet *temp);
    //temp is used to store my packets so that it can be validated
    
    void set_values(struct SPackets *packets, struct temp_Packet *temp){
        packets->_src[nPacket] = temp->src;
        packets->_dst[nPacket] = temp->dst;
        packets->_typ[nPacket] = temp->typ;
        packets->_prt[nPacket] = temp->prt;
    
    
        int _strlength = strlen(temp->data),i=0;
        for (i=0; i<_strlength; i++){
            packets->_data[nPacket][i] = temp->data[i];
        }
        //strcpy(packets->_data[nPacket], temp->data);
        printf("\nSrc: %i Dst: %i Typ: %i Prt: %i Data: %s\n",
               packets->_src[nPacket] , packets->_dst[nPacket], packets->_typ[nPacket] , packets->_prt[nPacket], packets->_data[nPacket]);
    }
    //end of SET_VALUES
    
    int new_packet(struct SPackets *packets, int goagain){
        char repeat='Y';
        goagain=nPacket;
        struct temp_Packet temp; //this is now the first time temp_packet is used so I can make do with dot operator I think?
        
        //get the input from the user
        scanf("%s", temp.buffer);
        scanf("%s", temp.buffer);
        scanf("%s", temp.buffer);
        scanf("%s", temp.buffer);
        scanf("%s", temp.data);
        
        //validate the data....
        temp.src = validate_string(temp.buffer, temp.src);
        temp.dst = validate_string(temp.buffer, temp.dst);
        temp.typ= validate_string(temp.buffer, temp.typ);
        temp.prt= validate_string(temp.buffer, temp.prt);
    
        //problems now happen when i attempt to pass the two structs into the set_values
        set_values(&packets, &temp);
        nPacket++;
    
    
        printf("\nPacket %i created.\n\nDo you wish to create another packet?\n: ", nPacket);
        scanf("%c", &repeat);
    
    
        if((repeat=='Y')||(repeat=='y')){
            return goagain++;
        } else {
            return goagain=51;
        }
    }
    //end of NEW_PACKET
    
    
    int main(){
    int goagain =0;
    
    
         while(nPacket<51){
              goagain=new_packet(&packets, goagain);
                     if(goagain==51){
                          goto exit_packetloop;
                        }
                    }
                    if (nPacket>50){
                        printf("\nMaximum packet limit reached...\nReturning to main menu...\n");
                    }
          }
          exit_packetloop: ;
    
          return 0;
    }
    /*
    error codes that i get "warning: passing argument 1 of 'set_values' from incompatible pointer type
                                  "note: expected 'struct SPackets *' but argument is of type 'struct SPackets **'
    */
    While I get that my problem is incorrect parameters, I'm not sure what I should do, I tried changing the parameters to:

    Code:
    void set_values(struct SPackets **packets, struct temp_Packet *temp);
    but that's still wrong.

    Any help would be appreciated, its been driving me up the wall this has.

    Regards,
    Shini
    Last edited by Shini; 05-10-2012 at 12:06 PM.

  7. #7
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    The errormessage doesn't make sense as it is unless you have another variable packets in the section of main that you stripped that shadows the global packets.
    Kurt

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    62
    Code:
    struct temp_Packet{
        int src, dst, typ, prt;
        char data[51], buffer[51];
    }temp;
    //end of temp_Packet
    
    struct SPackets{
        int _src[50], _dst[50], _typ[50], _prt[50];
        char _data[51][51];
    }packets;
    //end of SPackets
    Do you really need to setup the data structure that way? If you have to deal with single packets and a collection of packets, something like this would make it a lot easier:

    Code:
    struct packet_t {  /* single packet */
      int src, dst, typ, prt;
      char data[51], buffer[51];
    };
    
    struct packet_t packets[50]; /* array of packets */

  9. #9
    Registered User
    Join Date
    May 2012
    Posts
    9
    Well I've changed my code above so that it shows more or my code, the reason i didnt have it before was because I didnt know that the problem could've lied in main, thought it was just a problem with the parameters....

    Thanks for the response!
    Shini

    EDIT in response to rpet: Ah....that would've probably been a much better of doing this actually. Right, I'm going to do that now and we'll see how long that'll take me. Thanks!

    EDIT 2: Okay, I've changed the code so that its only just the one struct type and instead I have an array of these structs. Going to test to see if its working now, once again thanks!
    Last edited by Shini; 05-10-2012 at 12:24 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 05-30-2009, 11:04 PM
  2. passing struct to function
    By silentkarma in forum Windows Programming
    Replies: 15
    Last Post: 03-07-2008, 12:57 AM
  3. Passing array of struct as a function parameter
    By desmond5 in forum C Programming
    Replies: 5
    Last Post: 12-04-2007, 11:32 AM
  4. passing array of type struct to function
    By nappaji in forum C Programming
    Replies: 4
    Last Post: 05-02-2007, 05:13 PM
  5. passing struct to function
    By blight2c in forum C++ Programming
    Replies: 3
    Last Post: 03-16-2002, 12:52 AM