How to use array of pointers to print structure entities?
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("Heyn");
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("%sn",s[i].id);
++i;
}
return 0;
}
set 2:
#include<stdio.h>
typedef struct
{
char str[10];
int id;
}ak;
int main()
{
printf("Heyn");
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("%sn",s[i]);
++i;
}
return 0;
}
So when I tried set1 code,it gave me error saying:
C:CPPcPrototypes>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("%sn",*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
c
|
show 3 more comments
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("Heyn");
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("%sn",s[i].id);
++i;
}
return 0;
}
set 2:
#include<stdio.h>
typedef struct
{
char str[10];
int id;
}ak;
int main()
{
printf("Heyn");
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("%sn",s[i]);
++i;
}
return 0;
}
So when I tried set1 code,it gave me error saying:
C:CPPcPrototypes>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("%sn",*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
c
The error message means you’re using the dot operator where you should be using the arrow operator, or vice versa.
– Jonathan Leffler
Jan 1 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.
– Gerhardh
Jan 1 at 18:21
theset1
does not compile. How are we to advise you on run-time logic when you do not post code that compiles?
– user3629249
Jan 2 at 0:53
theset2
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=]
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
– user3629249
Jan 2 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.
– user3629249
Jan 2 at 0:59
|
show 3 more comments
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("Heyn");
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("%sn",s[i].id);
++i;
}
return 0;
}
set 2:
#include<stdio.h>
typedef struct
{
char str[10];
int id;
}ak;
int main()
{
printf("Heyn");
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("%sn",s[i]);
++i;
}
return 0;
}
So when I tried set1 code,it gave me error saying:
C:CPPcPrototypes>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("%sn",*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
c
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("Heyn");
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("%sn",s[i].id);
++i;
}
return 0;
}
set 2:
#include<stdio.h>
typedef struct
{
char str[10];
int id;
}ak;
int main()
{
printf("Heyn");
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("%sn",s[i]);
++i;
}
return 0;
}
So when I tried set1 code,it gave me error saying:
C:CPPcPrototypes>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("%sn",*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
c
c
edited Jan 1 at 15:50
Yunnosch
11.4k52034
11.4k52034
asked Jan 1 at 10:14
Athul KrishnanAthul Krishnan
6
6
The error message means you’re using the dot operator where you should be using the arrow operator, or vice versa.
– Jonathan Leffler
Jan 1 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.
– Gerhardh
Jan 1 at 18:21
theset1
does not compile. How are we to advise you on run-time logic when you do not post code that compiles?
– user3629249
Jan 2 at 0:53
theset2
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=]
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
– user3629249
Jan 2 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.
– user3629249
Jan 2 at 0:59
|
show 3 more comments
The error message means you’re using the dot operator where you should be using the arrow operator, or vice versa.
– Jonathan Leffler
Jan 1 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.
– Gerhardh
Jan 1 at 18:21
theset1
does not compile. How are we to advise you on run-time logic when you do not post code that compiles?
– user3629249
Jan 2 at 0:53
theset2
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=]
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
– user3629249
Jan 2 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.
– user3629249
Jan 2 at 0:59
The error message means you’re using the dot operator where you should be using the arrow operator, or vice versa.
– Jonathan Leffler
Jan 1 at 14:49
The error message means you’re using the dot operator where you should be using the arrow operator, or vice versa.
– Jonathan Leffler
Jan 1 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.
– Gerhardh
Jan 1 at 18:21
Please use Copy&Paste to show us the text error messages. They are no artwork and don't need to be shown as images.
– Gerhardh
Jan 1 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?– user3629249
Jan 2 at 0:53
the
set1
does not compile. How are we to advise you on run-time logic when you do not post code that compiles?– user3629249
Jan 2 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– user3629249
Jan 2 at 0:56
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– user3629249
Jan 2 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.
– user3629249
Jan 2 at 0:59
OT: the struct definition is missing a 'tag' name. Without a 'tag' name, most debuggers will not display the individual fields in the struct.
– user3629249
Jan 2 at 0:59
|
show 3 more comments
1 Answer
1
active
oldest
votes
Concerning set1 and your problems with printf("%sn",*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("%sn",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 %dn",s[i]->str, s[i]->id);
@AthulKrishnan do you understand the problems ?
– bruno
Jan 1 at 11:06
Got the output....Thank you guyz….
– Athul Krishnan
Jan 2 at 14:56
really....it was a silly mistake...
– Athul Krishnan
Jan 2 at 14:57
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53994635%2fhow-to-use-array-of-pointers-to-print-structure-entities%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Concerning set1 and your problems with printf("%sn",*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("%sn",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 %dn",s[i]->str, s[i]->id);
@AthulKrishnan do you understand the problems ?
– bruno
Jan 1 at 11:06
Got the output....Thank you guyz….
– Athul Krishnan
Jan 2 at 14:56
really....it was a silly mistake...
– Athul Krishnan
Jan 2 at 14:57
add a comment |
Concerning set1 and your problems with printf("%sn",*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("%sn",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 %dn",s[i]->str, s[i]->id);
@AthulKrishnan do you understand the problems ?
– bruno
Jan 1 at 11:06
Got the output....Thank you guyz….
– Athul Krishnan
Jan 2 at 14:56
really....it was a silly mistake...
– Athul Krishnan
Jan 2 at 14:57
add a comment |
Concerning set1 and your problems with printf("%sn",*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("%sn",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 %dn",s[i]->str, s[i]->id);
Concerning set1 and your problems with printf("%sn",*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("%sn",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 %dn",s[i]->str, s[i]->id);
edited Jan 2 at 6:13
answered Jan 1 at 10:25


brunobruno
9,69021126
9,69021126
@AthulKrishnan do you understand the problems ?
– bruno
Jan 1 at 11:06
Got the output....Thank you guyz….
– Athul Krishnan
Jan 2 at 14:56
really....it was a silly mistake...
– Athul Krishnan
Jan 2 at 14:57
add a comment |
@AthulKrishnan do you understand the problems ?
– bruno
Jan 1 at 11:06
Got the output....Thank you guyz….
– Athul Krishnan
Jan 2 at 14:56
really....it was a silly mistake...
– Athul Krishnan
Jan 2 at 14:57
@AthulKrishnan do you understand the problems ?
– bruno
Jan 1 at 11:06
@AthulKrishnan do you understand the problems ?
– bruno
Jan 1 at 11:06
Got the output....Thank you guyz….
– Athul Krishnan
Jan 2 at 14:56
Got the output....Thank you guyz….
– Athul Krishnan
Jan 2 at 14:56
really....it was a silly mistake...
– Athul Krishnan
Jan 2 at 14:57
really....it was a silly mistake...
– Athul Krishnan
Jan 2 at 14:57
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53994635%2fhow-to-use-array-of-pointers-to-print-structure-entities%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
The error message means you’re using the dot operator where you should be using the arrow operator, or vice versa.
– Jonathan Leffler
Jan 1 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.
– Gerhardh
Jan 1 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?– user3629249
Jan 2 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=]
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– user3629249
Jan 2 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.
– user3629249
Jan 2 at 0:59