Problem with structure

Programming, for all ages and all languages.
Post Reply
beyondsociety

Problem with structure

Post 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);
}
User avatar
df
Member
Member
Posts: 1076
Joined: Fri Oct 22, 2004 11:00 pm
Contact:

Re:Problem with structure

Post by df »

Code: Select all

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

Re:Problem with structure

Post 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.
Schol-R-LEA

Re:Problem with structure

Post 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.
Tim

Re:Problem with structure

Post by Tim »

Also, reading the compiler's errors and warnings would help. VC++ 6 will complain loudly at both of these source files.
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:Problem with structure

Post 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).
beyondsociety

Re:Problem with structure

Post by beyondsociety »

No wonder why I was having problems. Everything has been fixed and it works perfectly.

Thanks for the help.
Tim

Re:Problem with structure

Post by Tim »

Afaik the envp parameter is an extension to the standard, albeit a common one.
Perica
Member
Member
Posts: 454
Joined: Sat Nov 25, 2006 12:50 am

Re:Problem with structure

Post by Perica »

..
Last edited by Perica on Tue Dec 05, 2006 9:25 pm, edited 1 time in total.
Schol-R-LEA

Re:Problem with structure

Post 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.
Post Reply