Search code examples
arrayscstructimplicit-conversion

Unclear behavior of structs in a function


I am creating a project and I encountered behavior that is not supposed to happen (at least as far as I know). I am trying to change values in a struct array inside of a function addOrder. in main() I give struct's address to a function addOrder (and this is the first warning). Then I try to change its contents in the named function. I should only be able to change using ->, however, I get errors this way and I can only change using data[0].someName

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

#define MENU_TITLE "----Welcome to order manager!---" // menu title
#define MENU_SIZE 4 //change based on menu options
#define INPUT_MSG "Please choose one of the following options" //enter input message (range is added later automatically)
#define SIZE 25 //size of option message array
#define STREET_NAME_SIZE 50 //size of street name array
#define BUILDING_NUMBER_SIZE 10 //size of building number array
#define EMAIL_SIZE 40 //size of email array
#define INPUT_PHONE_MAX 12

typedef struct Data{

char streetName[STREET_NAME_SIZE];
char buildingNumber[BUILDING_NUMBER_SIZE];
unsigned long long phoneNumber;
char email[EMAIL_SIZE];

}Data;

void loadOrderAmount(int *orderAmount);
void showMenu(char *menuTitle, int menuSize, char *inputMsg);
int getChoise();
void addOrder(Data *data, int *orderAmount);
void getEmail(char *email);
unsigned long long getPhoneNumber();

int main(){
    int orderAmount = 0;
    loadOrderAmount(&orderAmount);
    Data data[orderAmount + 1];
    int choise = 0;

    showMenu(MENU_TITLE, MENU_SIZE, INPUT_MSG);
    while(!choise){
        choise = getChoise();
    }

    printf("\n");
    switch(choise){
        case 1:
            addOrder(&data,&orderAmount);   // This gives first warning
            break;
        case 2:
            //removeOrder(data);
            break;
        case 3:
            //editOrder(data);
            break;
        case 4:
            printf("Have a nice day\n");
            return 0;
    }
    printf("in main data %d\n",&data);
    //printf("%s %s ",data[0].streetName,data[0].buildingNumber);
    //printf("+%llu ",data[0].phoneNumber);
    printf("in main %s %llu\n",data[0].email,data[0].phoneNumber);
    return 0;
}

void addOrder(Data *data,int *orderAmount){

    int amount = *orderAmount; //it is equal to 0
    char email[41];
    char street[51];
    char buildingNumber[11];
    printf("Adding an order...\n\n");
    //++orderAmount;

   // getStreetName(data, &orderAmount);
   // getBuildingNumber(data,&orderAmount);
    data[amount]->phoneNumber = getPhoneNumber();   //why this does not work? 
    getEmail(email);
    strcpy(data[0].email,email); // why this works? its a function....
    printf("In function data %d\n", data);
    printf("in struct %s %llu\n",data[0].email, data[0].phoneNumber);
}
}

This is only a part of a code that is relevant and most functions are missing. Can anyone help me with this?


Solution

  • The function addOrder is declared like

    void addOrder(Data *data, int *orderAmount);
    

    But you are calling it passing the first argument of the type Data ( * )[orderAmount + 1]

    addOrder(&data,&orderAmount);
    

    You need to call it like

    addOrder( data, &orderAmount );
    

    Instead of this statement

    data[amount]->phoneNumber = getPhoneNumber();
    

    you have to write

    data[amount].phoneNumber = getPhoneNumber();
    

    And instead of this call

    printf("In function data %d\n", data);
    

    you have to write

    printf("In function data %p\n", ( void * )data);
    

    Pay attention to that within the function the expressions

    data[amount]
    

    and

    data[0]
    

    are not the same because amount is not equal to zero. The function is called with the argument orderAmount (passed by reference) that is assigned to amount.