A newbie question

D

dnucit

#include <stdio.h>

struct st{
int a;
char b;
};


int main()
{
struct st s;
s.a = 2;
printf("&s=%p s=%d &s.a=%p s.a=%d\n",&s,s,&s.a,s.a);
printf("&s=%p\n",&s);
printf("s=%d\n",s);
printf("&s.a=%p\n",&s.a);
printf("s.a=%d\n",s.a);
}

when i run the above program in my linux machine,i get

&s=0xfefc7a00 s=2 &s.a=0x2 s.a=-17008128
&s=0xfefc7a00
s=2
&s.a=0xfefc7a00
s.a=2

I'm a newbie.
Can someone explain this behaviour??
 
C

cp

Using the &s = %p, and referring %p to %s in your variable list you're
asking C to print out the hexidecimal addresses in the storage(RAM).
 
M

MQ

cp said:
Using the &s = %p, and referring %p to %s in your variable list you're
asking C to print out the hexidecimal addresses in the storage(RAM).

That's not the issue that is being questioned. It is the fact that
printf seems to print these values differently when they are on a
single line, and when they are on multiple lines. I'm interested to
find out why, as this also happens on my machine and I can't explain
it.
 
M

mdler

Hello

The error is in printing s
this is a struct, C does not allow to give it as argument in a
function.
In Mine implementation of c I get

error: STRUCT/UNION arguments are not supported.

on that line.

This explains also that all data after this is rubish

What is the value of a structure ????

Greetings Olaf

MQ schreef:
 
P

pemo

MQ said:
That's not the issue that is being questioned. It is the fact that
printf seems to print these values differently when they are on a
single line, and when they are on multiple lines. I'm interested to
find out why, as this also happens on my machine and I can't explain
it.

I think you'll find it's that the OP is lying - using %d when passing an
instance of struct st ... how many bytes will printf take off the stack
[or - for the pedants - whatever /thing/ is used to pass args to functions]
here - and where will printf /think/ the next arg starts in a multi-arg call
like this.

Take out the %d -> s stuff and try it again.
 
P

pete

MQ said:
That's not the issue that is being questioned. It is the fact that
printf seems to print these values differently when they are on a
single line, and when they are on multiple lines. I'm interested to
find out why, as this also happens on my machine and I can't explain
it.

Outputing a struct value as an int value,
causes undefined behavior.

struct st s;

printf("s=%d\n", s);

new.c runs fine

/* BEGIN new.c */

#include <stdio.h>

struct st{
int a;
char b;
};


int main(void)
{
struct st s;

s.a = 2;
printf("&s=%p &s.a=%p s.a=%d\n",
&s,
&s.a,
s.a);
printf("&s=%p\n", &s);
printf("&s.a=%p\n", &s.a);
printf("s.a=%d\n", s.a);
return 0;
}

/* END new.c */
 
M

Michael Brennan

#include <stdio.h>

struct st{
int a;
char b;
};


int main()
{
struct st s;
s.a = 2;
printf("&s=%p s=%d &s.a=%p s.a=%d\n",&s,s,&s.a,s.a);
printf("&s=%p\n",&s);
printf("s=%d\n",s);
printf("&s.a=%p\n",&s.a);
printf("s.a=%d\n",s.a);
}

when i run the above program in my linux machine,i get

&s=0xfefc7a00 s=2 &s.a=0x2 s.a=-17008128
&s=0xfefc7a00
s=2
&s.a=0xfefc7a00
s.a=2

I'm a newbie.
Can someone explain this behaviour??

Yes, this behavior is undefined,

because your arguments are not the correct types for the
conversion specifiers you have.

%p expects a void pointer, you should cast your arguments to (void *)
%d expects an int. You pass a whole structure.

/Michael Brennan
 
M

mdler

Correction
The struct has the name s not c

printf("&s=%p s=%d &s.a=%p s.a=%d\n",
&s,
s, /* is struct not allowed as parameter in a function */
&s.a,
s.a);

After a bad argument it is not defined how the compiler fills in next
arguments!

Visual C 6 gives me no warning or error.

greetings Olaf

mdler schreef:
 
J

Jack Klein

Hello

The error is in printing s
this is a struct, C does not allow to give it as argument in a
function.

Of course you are allowed to pass a structure by value in C.
In Mine implementation of c I get

error: STRUCT/UNION arguments are not supported.

Whatever your implementation of, it is not C. Passing structs by
value has been part of the language standard for 17 years. And it was
mentioned as a common extension 29 years ago, in the first edition of
K&R. I do not know what it is you think that you have, but it is NOT
a C compiler.
 
B

Bob Nelson

#include <stdio.h>

struct st{
int a;
char b;
};

int main()
{
struct st s;
s.a = 2;
printf("&s=%p s=%d &s.a=%p s.a=%d\n",&s,s,&s.a,s.a);
printf("&s=%p\n",&s);
printf("s=%d\n",s);
printf("&s.a=%p\n",&s.a);
printf("s.a=%d\n",s.a);
}

when i run the above program in my linux machine,i get

&s=0xfefc7a00 s=2 &s.a=0x2 s.a=-17008128
&s=0xfefc7a00
s=2
&s.a=0xfefc7a00
s.a=2

The printf format specifiers do not match the argument types.

Compile with ``gcc -Wall -W -ansi -pedantic'' (or whatever your
platform's compiler's flags are for maximum warnings) to indenfity
each mismatch.
 

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,777
Messages
2,569,604
Members
45,208
Latest member
RandallLay

Latest Threads

Top