ansi c

R

Richard Bos

#include stio.h
struct{int integer; char caracter;} realstruct;
int main()
{
realstruct.integer = 1;
realstruct.caracter = "someword";
}
printf("%d %c", realstruct.integer, realstruct.caracter)
system("pause");

The problem is the output of printf realstruct.caracter, i see a
strange character in a command prompt, i tried also to use %s but i
got compiler error.
Have you some advice?

Yes. Post real code. Don't retype - copy and paste, including any
compiler messages. There are several things that _could_ be wrong with
your code (most likely being missing indirections or the wrong output
specifier), but as it stands, it cannot possibly compile let alone give
output, and it is impossible to determine which of the various possible
errors in your code is the real one.

Richard
 
Y

yqyq22

Dear all.
i have a little problem with this code, i use visual studio2005 (ansi
c)

#include stio.h
struct{int integer; char caracter;} realstruct;
int main()
{
realstruct.integer = 1;
realstruct.caracter = "someword";
}
printf("%d %c", realstruct.integer, realstruct.caracter)
system("pause");

The problem is the output of printf realstruct.caracter, i see a
strange character in a command prompt, i tried also to use %s but i
got compiler error.
Have you some advice?
thanks a lot
 
M

Mark Bluemel

Dear all.
i have a little problem with this code, i use visual studio2005 (ansi
c)

#include stio.h

Do mean "stdio.h"? You should really use cut and paste to post your real
code...
struct{int integer; char caracter;} realstruct;

So the structure called realstruct can contain an integer and a single
character...
int main()
{
realstruct.integer = 1;

You successfully assigned an integer into the structure.
realstruct.caracter = "someword";

is "someword" a single character?

No.

Then it won't be dealt with properly will it? What will happen is that
the pointer ("someword" in this context will be evaluated as a pointer)
will be converted to an integer value (in some manner) and that value
assigned into the char "caracter".

Didn't the compiler give you any warnings?
}
printf("%d %c", realstruct.integer, realstruct.caracter)
system("pause");

The problem is the output of printf realstruct.caracter, i see a
strange character in a command prompt, i tried also to use %s but i
got compiler error.

If you wanted to use "%s" you'd need "caracter" to be a pointer or an
array of characters.
Have you some advice?

Try using a better textbook?
 
J

Jensen Somers

Dear all.
i have a little problem with this code, i use visual studio2005 (ansi
c)

Note that Visual Studio 2005 (and 2008) roughly support ISO C89 and
don't include much of the newer available features and data types.
#include stio.h
struct{int integer; char caracter;} realstruct;

I assume this "caracter" field is an array?
int main()
{
realstruct.integer = 1;
realstruct.caracter = "someword";

Where is the declaration of the local variable? And you are putting a
string into a single char.
}
printf("%d %c", realstruct.integer, realstruct.caracter)
system("pause");

Shouldn't this be inside main()?
The problem is the output of printf realstruct.caracter, i see a
strange character in a command prompt, i tried also to use %s but i
got compiler error.
Have you some advice?
thanks a lot

Since this code is wrong at so many levels and does not compile like
this we cannot give accurate answers, so please provide the correct code.

- Jensen
 
Y

yqyq22

Note that Visual Studio 2005 (and 2008) roughly support ISO C89 and
don't include much of the newer available features and data types.




I assume this "caracter" field is an array?


Where is the declaration of the local variable? And you are putting a
string into a single char.


Shouldn't this be inside main()?




Since this code is wrong at so many levels and does not compile like
this we cannot give accurate answers, so please provide the correct code.

- Jensen

#include <stdio.h>

struct tagstruttura{
int serie;
char organizzatore;
int partecipanti;
} struttura;

int main()
{

struttura.serie = 2;
struttura.organizzatore = "cazzo";
struttura.partecipanti = 100;
printf("%d\n %c\n %d\n %X\n",
struttura.serie,struttura.organizzatore,struttura.partecipanti);
system("pause\n");


I don't got any warnings in my compiler.
thanks a lot
 
Y

yqyq22

#include <stdio.h>

        struct tagstruttura{
                            int serie;
                            char organizzatore;
                            int partecipanti;
                          } struttura;

int main()
{

        struttura.serie = 2;
        struttura.organizzatore = "cazzo";
        struttura.partecipanti = 100;
        printf("%d\n %c\n %d\n %X\n",
struttura.serie,struttura.organizzatore,struttura.partecipanti);
        system("pause\n");

I don't got any warnings in my compiler.
thanks a lot- Nascondi testo tra virgolette -

- Mostra testo tra virgolette -

I forgot........
if i change %c in %s tha compiler give me this Unhandled exception at
0x10227c2f (msvcr80d.dll) in testc.exe: 0xC0000005: Access violation
reading location 0x00000060.

thanks
 
M

Mark Bluemel

#include <stdio.h>

struct tagstruttura{
int serie;
char organizzatore;

"organizzatore" has storage for a single character.
int partecipanti;
} struttura;

int main()
{

struttura.serie = 2;
struttura.organizzatore = "cazzo";

What makes you think this is valid? What text book are you referring to?
struttura.partecipanti = 100;
printf("%d\n %c\n %d\n %X\n",
struttura.serie,struttura.organizzatore,struttura.partecipanti);

What do you expect to come out? Why?

Where is the value which the "%X" is intended to format?

For a quick fix, which may not be what you really want, try :-

#include <stdio.h>

struct tagstruttura{
int serie;
char *organizzatore; /* note the '*' */
int partecipanti;
} struttura;

int main()
{

struttura.serie = 2;
struttura.organizzatore = "cazzo";
struttura.partecipanti = 100;
printf("%d\n %s\n %d\n", /* note "%s" not "%c"
* and I've removed "%X"
*/
struttura.serie,struttura.organizzatore,struttura.partecipanti);
system("pause\n");
}
 
Y

yqyq22

"organizzatore" has storage for a single character.




What makes you think this is valid? What text book are you referring to?


What do you expect to come out? Why?

Where is the value which the "%X" is intended to format?

For a quick fix, which may not be what you really want, try :-

#include <stdio.h>

        struct tagstruttura{
                            int serie;
                            char *organizzatore; /* note the '*' */
                            int partecipanti;
                          } struttura;

int main()
{

        struttura.serie = 2;
        struttura.organizzatore = "cazzo";
        struttura.partecipanti = 100;
        printf("%d\n %s\n %d\n",      /* note "%s" not "%c"
                                        * and I've removed "%X"
                                        */
struttura.serie,struttura.organizzatore,struttura.partecipanti);
        system("pause\n");



}- Nascondi testo tra virgolette -

- Mostra testo tra virgolette -

thanks, it works, but why i must use "*" pointer? i forgot to remove
%X from my code ( i'm trying a lot of example in the same main() :)
thanks again, i'm a newbye but c is wonderful........
 
P

Philip Potter

Jensen said:
I assume this "caracter" field is an array?


Where is the declaration of the local variable?

If you mean "Where is realstruct declared?", the answer is this line
above main:
 
R

Rachael

thanks, it works, but why i must use "*" pointer?

Because the char type only stores single characters, like 'a' or 'b'.
Pointer types store locations in memory.

If you do
char c = 'a';
you are writing into the variable c the value of the character
'a' (which is just a number).

If you do
char * pc = "cazzo";
you are writing into the variable pc (which is a char pointer) the
*location* of the beginning of the string "cazzo", which is stored in
memory somewhere. You are not storing the whole string "cazzo" in pc,
because no single variable (i.e. not an array) in C can store a whole
string.

(Note single quotes for characters and double quotes for string
literals)
 
S

santosh

thanks, it works, but why i must use "*" pointer?

Without the * 'organizzatore' is simply declared as a char. A char can
only store a single character of the implementation's character set
like say, 'v' or '<' etc.

With the * 'organizzatore' is declared as an object of type char *,
which can point to one or more characters. And that is exactly what the
line:

struttura.organizzatore = "cazzo";

in main() does. It sets 'organizzatore' to point to an anonymous array
of char which contains the string "cazzo".

If these terms confuse you, then you need to read the first few chapters
of whatever textbook you are using. Pointers, arrays and strings are
quite subtle in C and a newsgroup is no place to adequately explain
them.

<snip>
 
S

santosh

Rachael said:
Because the char type only stores single characters, like 'a' or 'b'.
Pointer types store locations in memory.

If you do
char c = 'a';
you are writing into the variable c the value of the character
'a' (which is just a number).

If you do
char * pc = "cazzo";
you are writing into the variable pc (which is a char pointer) the
*location* of the beginning of the string "cazzo", which is stored in
memory somewhere. You are not storing the whole string "cazzo" in pc,
because no single variable (i.e. not an array) in C can store a whole
string.

If you don't mind about portability or sanity you can stuff a small
enough string into all the basic C types.

<snip>
 
D

dj3vande

santosh said:
If you don't mind about portability or sanity you can stuff a small
enough string into all the basic C types.

If your string is small enough, you can even do it portably (though the
sanity barrier does stay intact).
--------
#undef NDEBUG
#include <assert.h>
#include <string.h>

/*This typedef can refer to any complete type you like*/
typedef double *my_type;

int main(void)
{
my_type b;
char *bogus_string;

/*Icky pointless code, but completely legal and portable*/
bogus_string=(char *)&b;
strcpy(bogus_string,"");

/*assert will never fire*/
assert(strcmp(bogus_string,"")==0);

return 0;
}
 
M

Martin Ambuhl

Dear all.
i have a little problem with this code, i use visual studio2005 (ansi
c)

I tremble at the thought that I am feeding a troll, since it is almost
inconceivable that your 'code' could have been written in good faith.
#include stio.h
^^^^^^^^^^^^^^^^
That line is, obviously, gibberish.
struct{int integer; char caracter;} realstruct;
^^^^^^^^^^^^^
Here you specify that the 'caracter' member holds a single character.
int main()
{
realstruct.integer = 1;
realstruct.caracter = "someword";
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Here you try to assign (a pointer to) a literal string into a single
character. This is, rather obviously, a gross error.

At this point, the closing '{' for main has been reached. Every
statement after that brace is outside a function and an obvious error.
printf("%d %c", realstruct.integer, realstruct.caracter)

Leaving off semicolons is not very smart.
system("pause");

The problem is the output of printf realstruct.caracter,

No, the problem is that you don't understand what a char is. Or even
what a C program is. Or even what a programming language is.
i see a
strange character in a command prompt, i tried also to use %s but i
got compiler error.

Be glad your compiler told you that a char is not a string. It didn't
have to.
Have you some advice?

a) Pay attention to the _contents_ of the diagnostic message, not just
that you have one.
b) Get an elementary C textbook and start reading at the first page.
Programming is not something you do by typing random characters and then
complaining when they don't result in a compilable program.

Compare your code to

[1]
#include <stdio.h>

struct
{
int integer;
char *caracter;
} realstruct;

int main(void)
{
realstruct.integer = 1;
realstruct.caracter = "someword";

printf("%d %s\n", realstruct.integer, realstruct.caracter);
return 0;
}

or
[2]
#include <stdio.h>

struct test_struct
{
int integer;
char *caracter;
};

int main(void)
{
struct test_struct realstruct = { 1, "someword" };
printf("%d %s\n", realstruct.integer, realstruct.caracter);
return 0;
}

or, if you have C99-style initializations, so you need know only the
relevant member names
[3]
#include <stdio.h>

struct test_struct
{
int integer;
char *caracter;
};

int main(void)
{
struct test_struct realstruct = {.integer = 1,.caracter =
"someword" };
printf("%d %s\n", realstruct.integer, realstruct.caracter);
return 0;
}



If you don't know the relevant string at compile time that you want
caracter to point to, then you have more problems. But, frankly, you
are nowhere close to being able to understand those problems, much less
how to solve them.
 
A

Army1987

yqyq22 said:
#include <stdio.h>

struct tagstruttura{
int serie;
char organizzatore;
int partecipanti;
} struttura;

int main()
{

struttura.serie = 2;
struttura.organizzatore = "cazzo";
What the f*** is being organized? Probably right that...
struttura.partecipanti = 100;
Wow, it'll be a somewhat big orgy...

(For non-Italian speakers, look up "cazzo" somewhere...)
printf("%d\n %c\n %d\n %X\n",
struttura.serie,struttura.organizzatore,struttura.partecipanti);
system("pause\n");


I don't got any warnings in my compiler.
Then raise the warning level. It allowed you to store a pointer to char in
a char.
 
Y

yqyq22

What the f*** is being organized? Probably right that...>    struttura..partecipanti = 100;

Wow, it'll be a somewhat big orgy...

(For non-Italian speakers, look up "cazzo" somewhere...)



Then raise the warning level. It allowed you to store a pointer to char in
a char.

THANKS TO ALL. now is clear
 

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,995
Messages
2,570,228
Members
46,817
Latest member
AdalbertoT

Latest Threads

Top