Page 1 of 1

Problem with structure

Posted: Fri Jan 16, 2004 8:44 pm
by beyondsociety
Im trying to fix some code and I cant figure out why one version of this code works but the other doesnt. Both versions compile and link fine, but only one works when I test it with Microsoft VC++ 6.0.

1st version which works : Prints fine to screen

Code: Select all

struct date
{
   int day;
   int month;
   int year;
}

int main(void)
{
   struct date d;

   d.month = "December";
   d.day = 30;
   d.year = 1982;
    
   printf("Month = %s, Day = %d, Year = %d\n", d.month, d.day, d.year);
}
2nd version which doesnt work : VC++ dumps with a "You tried to access a referenced memory address, memory could not be written to" error.

Code: Select all

typedef struct 
{
   int day;
   int month;
   int year;
}date;

int main(date *d)
{
   d->month = "December";
   d->day   = 30;
   d->year  = 1982;

   printf("My birthday is on %s / %d / %d\n", d->month, d->day, d->year);
}

Re:Problem with structure

Posted: Sat Jan 17, 2004 5:48 am
by df

Code: Select all

d.month = "December";
what is base variable type is month? ;D

Re:Problem with structure

Posted: Sat Jan 17, 2004 6:02 am
by Tim

Code: Select all

int main(date *d)
What is this?

Remember that main can have one of only two forms (well, three on some compilers):

Code: Select all

int main(void)
or:

Code: Select all

int main(int argc, char **argv)
You have effectively invented a new type of main and are expecting the C runtime to deal with it. Instead, the C runtime is passing int argc where you expect a pointer to a date structure.

Re:Problem with structure

Posted: Sat Jan 17, 2004 12:54 pm
by Schol-R-LEA
df wrote:

Code: Select all

d.month = "December";
what is base variable type is month? ;D
Lucky for him that the x86 uses a 32-bit value for both ints and pointers. On an Itanic or an UltraSPARC, it would be seriously hosed. The compiler should have warned about the bad cast from char* to int, and probably did (both VC++ 6.0 and gcc did when I tested it). Fortunately, literal strings are stored as globals, so there are no wild pointer problems, in this case. Had he read the string in with, say, scanf(), however, there would have hell to pay.

As for the original question, Tim-sensei has the right of it; main() is a special case, and can only take an int followed by a pointer to an array or char pointers as parameters (these are for accepting command-line arguments; the int is the number of arguments, while the arguments themselves are stored in the array of strings, with *argv[[0]] == the name of the program). Among other things, this is one of the reasons why Windows uses a function WinMain() at the start of Win32 API programs instead of just a main(); the main() function is there, IIRC, but because the entry point of a Windows program needs certain arguments and initializations, main() is automagically generated to handle the setup needed by WinMain().

BTW, you need to have a semicolon at the end of the struct declaration, are else it would look like it's part of the return type form main(). Adding an "[tt]#include <stdio.h>[/tt]" at the top would help, too. HTH. C&CW.

Re:Problem with structure

Posted: Sat Jan 17, 2004 2:27 pm
by Tim
Also, reading the compiler's errors and warnings would help. VC++ 6 will complain loudly at both of these source files.

Re:Problem with structure

Posted: Sat Jan 17, 2004 2:27 pm
by Candy
Tim Robinson wrote: Remember that main can have one of only two forms (well, three on some compilers):

Code: Select all

int main(void)
or:

Code: Select all

int main(int argc, char **argv)
Well, the complete main is still

Code: Select all

int main(int argc, char **argv, char **envp)
It's just that you can leave out one or more of the arguments, as is standard C (afaik). It would just ignore the extra arguments given (and the compiler would not complain, since the call is precompiled).

Re:Problem with structure

Posted: Sat Jan 17, 2004 2:37 pm
by beyondsociety
No wonder why I was having problems. Everything has been fixed and it works perfectly.

Thanks for the help.

Re:Problem with structure

Posted: Sat Jan 17, 2004 3:44 pm
by Tim
Afaik the envp parameter is an extension to the standard, albeit a common one.

Re:Problem with structure

Posted: Sun Jan 18, 2004 1:18 am
by Perica
..

Re:Problem with structure

Posted: Sun Jan 18, 2004 2:33 am
by Schol-R-LEA
'envp' stands for 'environment pointer' or 'environment parameter'. It is an implicit argument that holds a list of the local environment variables stored as strings, IIUC.