I wrote a program in C which has a structure named ak.
There is an array of pointers s which stores the address of array p of ak type. After inputting the values, only str is getting printed.
How can I print both str and id using array of pointers?
set 1:
#include<stdio.h>
typedef struct
{
char str[10];
int id;
}ak;
int main()
{
printf("Hey\n");
int i;
ak *s[5],p[5];
for(i=0;i<5;i++)
{
s[i]=&p[i];
printf("Input string:");
scanf("%s",&p[i].str);
printf("Input id:");
scanf("%d",&p[i].id);
}
i=0;
while(i<5)
{
printf("%s\n",s[i].id);
++i;
}
return 0;
}
set 2:
#include<stdio.h>
typedef struct
{
char str[10];
int id;
}ak;
int main()
{
printf("Hey\n");
int i;
ak *s[5],p[5];
for(i=0;i<5;i++)
{
s[i]=&p[i];
printf("Input string:");
scanf("%s",&p[i].str);
printf("Input id:");
scanf("%d",&p[i].id);
}
i=0;
while(i<5)
{
printf("%s\n",s[i]);
++i;
}
return 0;
}
So when I tried set1 code,it gave me error saying:
C:\CPP\c\Prototypes>gcc -o ct structure.c
structure.c: In function 'main':
structure.c:22:32: error: request for member 'id' in something not a structure or union
printf("%s\n",*s[i].id);
^
Screenshot is here:
https://imageshack.com/a/img921/3084/j1rHig.png
When I tried set2 code, it only printed str values. screenshot is here:
set1does not compile. How are we to advise you on run-time logic when you do not post code that compiles?set2results in the compiler giving two (serious) warnings:untitled.c:17:18: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[10]’ [-Wformat=]anduntitled.c:24:19: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘ak * {aka struct <anonymous> *}’ [-Wformat=]Both of these problems need to be corrected, or the code will not act as desired when run