Skip to main content
gamedev.net
🔒 Locked

Pointer dereferencing fun!

Started by Darkeye Nov 19, 2001 at 5:25 PM 6 replies 350+ views
Original Post
Darkeye
Darkeye
Ok, I lied in the subject, this isn''t fun at all. Basically, I need to pass a string to a function and allow that function to modify the string. At the moment, this is what I have: char *category; memset( &category, 0, sizeof( category ) ); That is the string I need to pass and modify in the other function. get_submitted_question( index, submit_filename, &category, etc etc. That is the call to the function that needs to modify the string. (The function takes a char** as the param) Then, in that function, whenever I try to modify the string by dereferencing it, for example: strcpy((*category), "Category"); The program crashes with: Unhandled exception at 0x004033eb in Quizbot.exe: 0xC0000005: Access violation writing location 0x00000000 Any ideas would be welcome.
thuned
thuned
char *category isn''t really a string. its a pointer to a char (which could be a string, but wouldn''t work here).
try changing it to
char category[80];
life is unfair, take advantage of it.UNMB2 - if the link doesn't work, try clicking it :)
Red Ant
Red Ant
You have to make sure that the memory allocated to category is large enough to actually contain "Category" plus the terminating ''\0''.
Pactuul
Pactuul
char *category;
memset( &category, 0, sizeof( category ) );

well all you have is a pointer
and when you call sizeof (category) this returns the size of the pointer which is like 4 bytes ( i believe), but your using &category, which is partially correct, but your zeroing out only first 4 bytes.


you need to do this

char* category
category = new char[MAXIUM_SIZE_EVER];
memset(category, 0, sizeof (category));

//then
strcpy(category, "Category");

this should work now
Pactuul
-Pac "The thing I like about friends in my classes is that they can't access my private members directly." "When listening to some one tell about their problem (whether it's code or not), don't listen to what went right or wrong, but what they assumed....."
Fruny
Fruny
Access violation writing location 0x00000000

That''s a NULL pointer you''re trying to write on.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Mayrel
Mayrel
Assuming he''s using C++, Null and Void.

Much the same is:

  
char *category;
category = malloc(sizeof(char) * size_wanted);
or, in c++
category = new char[size_wanted];
memset(category, 0, size_wanted);


You don''t need to change the strcpy line - it should be correct as it is (because category is a char** in get_submitted_question).

  
strcpy((*category), "Category");


All your bases belong to us (I know. It''s irony.)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.