![]() |
| | #1 |
| Registered User Join Date: Dec 2008
Posts: 3
| New to c, help with (void *) I'm pretty new to this, so bear with me. I'm having a few problems accessing data in a void *. Basically, we had to convert a program that created and performed functions on a linked list of integers to one that used void *, and then could handle any data type. This is part of my main.c (I'm running it redirecting the input to a file that contains a list of numbers..."main.x < numbers"): Code: int main() {
int cur_size, *number_read;
void *occurrences, *large_value, *large_key, **key_list;
Table list= create();
while (!feof(stdin)) {
scanf("%d ", number_read);
if (lookup(list, number_read, &occurrences) == 1) {
delete(&list, number_read);
insert(&list, number_read, occurrences); /* needs to be occurrences + 1 */
} else insert(&list, number_read, (void *)1);
}
My problem is with the insert function. First, is the "else insert(...)" call correct? It should be inserting a pointer to the integer value 1. Second, how do I send in a pointer to the integer value stored at *occurrences + 1? The call to lookup(...) in the if will store a value in occurrences if it finds number_read in the list. I've tried casting it to int *, then dereferencing it but I get a seg fault anytime I try to do that. Any help is awesome...I know this post is probably extremely confusing but I don't have a better way to write it... Thanks in advance! |
| new to c is offline | |
| | #2 |
| Registered User Join Date: Jan 2008 Location: Seattle
Posts: 476
| What does the prototype look like for your insert function call? Also passing variables like that is not best practice unless strictly for testing, you should actually be passing a variable and proper type. Also the real use of void* is to cast to a different type. If you happen to know that you need to cast the response from occurrences variable I would do that somewhere else, but not as a declaration. |
| slingerland3g is offline | |
| | #3 |
| Registered User Join Date: Dec 2008
Posts: 3
| Thank you so much for the quick reply! The insert function looks like this: Code: int insert(Table *table, void *key, void *item) |
| new to c is offline | |
| | #4 |
| Registered User Join Date: Jun 2008
Posts: 1,134
| I don't see the logic of (void*)1. A void* stores a memory address, as all pointers. So you are casting 1 to a memory address? Meaning that you the memory address with value 1?? That makes no sense. You would do this somewhere: Code: int one = 1; void* p = &one; insert(..., ..., p); I dont think either this is correct Code: lookup(list, number_read, &occurrences) |
| C_ntua is offline | |
| | #5 |
| Registered User Join Date: Dec 2008
Posts: 3
| Haha no I'm not sure why I haven't really questioned the (void *)1 thing yet...thank you for pointing that out to me. And lookup requires void ** as it's third parameter...lookup doesn't know what type it is dealing with, and since I can't use void as a data type, I have to work with void *. I didn't explain that very well... Ok so I made a couple changes and now it is running without any segfaults! Thank you for the help, if I can't get it running correctly soon I will definitely be back! thanks |
| new to c is offline | |
| | #6 | ||
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| Quote:
You are passing the address of a local variable which is then stored and later the local variable goes out of scope.
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| ||
| Elysia is offline | |
![]() |
| Tags |
| pointer, void |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Another syntax error | caldeira | C Programming | 31 | 09-05-2008 01:01 AM |
| 30something GSOH seeks help with the basics of a minor programme | promsan | C Programming | 3 | 05-13-2007 08:55 AM |
| game window rejected painting ! | black | Windows Programming | 4 | 03-27-2007 01:10 AM |
| msvc just ate one of my source files | Eber Kain | C++ Programming | 6 | 07-01-2004 05:40 AM |
| Half-life SDK, where are the constants? | bennyandthejets | Game Programming | 29 | 08-25-2003 11:58 AM |