address of struct_ptr member

G

GSA

I have a struct, say,

typedef struct {
int sa ;
char sb[10] ;

} struct1;
struct1 *s1;
s1->sa = 10;
s1->sb = "aChar";

But printf below gives me an error.

printf("sa = %d\n addr of sa = %d\n sb = %s\naddr of sb = %d\n", s1-
sa, &(s1->sa), s1->sb, &(s1->sb));


How do I fix this? Also,I donot want to use the '++' operator on
pointer struct1.
 
B

Ben Bacarisse

GSA said:
I have a struct, say,

typedef struct {
int sa ;
char sb[10] ;

} struct1;
struct1 *s1;
s1->sa = 10;
s1->sb = "aChar";

You should have got an error message here. You can't assign to an array
in C and s1->sb has array type.
But printf below gives me an error.

printf("sa = %d\n addr of sa = %d\n sb = %s\naddr of sb = %d\n",
s1->sa, &(s1->sa), s1->sb, &(s1->sb));

You can't use %d to print pointers. printf has a format for that: %p.
for technical reasons you need to cast the corresponding argument to
(void *).

However, none of this is as significant as the fact that s1 does not
point to an object of the right type. s1->sa is undefined unless you
arrange for s1 to point to the right kind of "thing" first (either to a
declared object of type struct1 or to some allocated space that is large
enough). [If you do do this and the above is in fact just a sketch,
then please post actual code in the future.]
How do I fix this? Also,I donot want to use the '++' operator on
pointer struct1.

This confuses me. If you don't want to use ++, don't use it. Why do
you think you might have to use it? Why would you not want to use it if
it turned out to the correct thing to do?
 
S

Seebs

But printf below gives me an error.
printf("sa = %d\n addr of sa = %d\n sb = %s\naddr of sb = %d\n", s1-

%d prints ints.
&(foo) is not an int, no matter what foo is.

Maybe you should try "%p" and "(void *) &(s1->sa)" and see whether that's
better.

Also: Your question was badly asked. Here's the most crucial thing:
*YOU DID NOT TELL US WHAT THE ERROR WAS*. That means that instead of
a detailed explanation of the error you got, you get my response to the
first thing that strikes me as suspicious, which may or may not be right.

I don't even know whether you got a compile-time error or a runtime error.

I don't even know whether by "error" you mean "output you didn't expect"
or something else.

I deleted a line about ++, but that also made no sense, because it had no
referent.

Please read this before asking further questions. You will get better
answers and people will like you better:
http://www.catb.org/esr/faqs/smart-questions.html

-s
 
G

GSA

Dear both,
Thank you for your time.
I got the precious clue "%p" and "(void *) &(s1->sa) from your advice.

My code was disactrous without 'malloc' mainly!!!

Also,I said I donot want to use the '++' operator on pointer to
struct1 as I wanted to access the address of the members using their
names and not pointer arithmetic. This is helpful if I am not exposed
to the structure and hence donot know the order of elements, but still
know the names of members.

I shall go through the suggested faq.

Regards,
GSA
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top