0

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.

2
  • 1
    Please make a habit of adding actual and desired output and/or comments indicating which lines print what and what they should print. Commented Mar 13, 2013 at 5:49
  • Sorry buddy ...i will follow that next time when i post...thanks for your advice Commented Mar 13, 2013 at 5:59

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks friend...i am such a stupid that i forgot the basics ...thanks a lot!
@studyembedded , everyone makes mistakes , thats how learning happens !!
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.