studyembedded
studyembedded

Reputation: 73

Getting location address instead of value while printing using pointers with structures?

I have this structure and using pointers , I am trying to print the values of different structure variables , but for int variables it prints location address instead of value and for char variables the result is correct.

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


struct calc{

    int row;
    int col;
    int menu_name[20];
    int sub_menu_name[20]; 

    };




int main()
{
int count = 0, i = 0;

struct calc *P_calc[2];

//p_calc = (struct calc *)malloc(sizeof(struct calc)*2);


for(count; count<1; count++)
{
      P_calc[count] = (struct calc *)malloc(sizeof(struct calc));

       printf("Please enter the row cordinates: \n");

       scanf("%d",P_calc[i]->row);
       printf("Please enter the col cordinates: \n");

       scanf("%d",P_calc[i]->col);
       printf("Please enter the menu_name: \n");

       scanf("%s",P_calc[i]->menu_name);
       printf("Please enter the sub_menu_name: \n");

       scanf("%s",P_calc[i]->sub_menu_name);


}

   for(i; i<1; i++)  
       {        
        printf("row : %d\n",P_calc[i]->row);

        printf("col :%d\n",P_calc[i]->col);

        printf("menu_name: %s\n",P_calc[i]->menu_name);

        printf("sub_menu_name :%s\n",P_calc[i]->sub_menu_name);
        }


system("PAUSE");
return 0;    
}

Please Help Me.

Thanks in Advance.

Upvotes: 0

Views: 548

Answers (2)

Ed Heal
Ed Heal

Reputation: 60007

Here are your problems:

  1. The structure should be

    struct calc{
    
    int row;
    int col;
    char menu_name[20];
    char sub_menu_name[20]; 
    
    };
    
  2. scanf("%d",P_calc[i]->row);should be scanf("%d",&P_calc[i]->row);

  3. scanf("%d",P_calc[i]->col);should be scanf("%d",&P_calc[i]->col);

It is also a good idea to check the return value from scanf.

Upvotes: 3

uba
uba

Reputation: 2031

The scanf statement should be

scanf("%d",&P_calc[i]->row);

The address needs to be passed to scanf. Similarly while reading the col variable.

Upvotes: 0

Related Questions