0

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:

https://imageshack.com/a/img922/614/JHSGZ9.png

8
  • The error message means you’re using the dot operator where you should be using the arrow operator, or vice versa. Commented Jan 1, 2019 at 14:49
  • Please use Copy&Paste to show us the text error messages. They are no artwork and don't need to be shown as images. Commented Jan 1, 2019 at 18:21
  • the set1 does not compile. How are we to advise you on run-time logic when you do not post code that compiles? Commented Jan 2, 2019 at 0:53
  • the set2 results 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=] and untitled.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 Commented Jan 2, 2019 at 0:56
  • OT: the struct definition is missing a 'tag' name. Without a 'tag' name, most debuggers will not display the individual fields in the struct. Commented Jan 2, 2019 at 0:59

1 Answer 1

2

Concerning set1 and your problems with printf("%s\n",*s[i].id);.

*s[i].id is equivalent to *(s[i].id), not to (*s[i]).id as you probably supposed. Because the type of s[i] is ak * you cannot get its field id through s[i].id.

You can write (*s[i]).id but a more readable way is to use s[i]->id.

Your printf has an other problem, the format cannot be "%s" because s[i]->id is an int, not a char *


Concerning set2

You do printf("%s\n",s[i]);, and you are surprised because only the string is printed, how can you expect that print the string and the int ?

You request to print a string (format %s) but s[i] is not a string. By chance the struct starts by the field str being a string, so yes you write it, but this is not the right way.

You have to explicitly print each attributes, for instance doing printf("%s %d\n",s[i]->str, s[i]->id);

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

1 Comment

@AthulKrishnan do you understand the problems ?

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.