C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 12-03-2008, 09:57 PM   #1
Registered User
 
Join Date: Dec 2008
Posts: 3
New to c, help with (void *)

Hi,

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);
  }
All of the functions it calls I'm pretty sure work, because they've worked in tests that I haven't written the main function. they work with strings, doubles, ints, etc.

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   Reply With Quote
Old 12-03-2008, 10:07 PM   #2
Registered User
 
slingerland3g's Avatar
 
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   Reply With Quote
Old 12-03-2008, 10:15 PM   #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)
So I should declare occurrences as int *?
new to c is offline   Reply With Quote
Old 12-03-2008, 10:26 PM   #4
Registered User
 
C_ntua's Avatar
 
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);
that would make sense

I dont think either this is correct
Code:
lookup(list, number_read, &occurrences)
I mean, won't you store how many occurrences there where? So you just need to pass a int*. Or a void* (for whatever reason). So you would pass occurrences (WITHOUT the &). You pass void**. But of course you know best, probably you want something else and I am getting it wrong.
C_ntua is offline   Reply With Quote
Old 12-03-2008, 10:58 PM   #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   Reply With Quote
Old 12-04-2008, 02:46 AM   #6
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
Quote:
Originally Posted by C_ntua View Post
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);
that would make sense.
...That would be a bad idea, as you should be aware of.
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:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Reply

Tags
pointer, void

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 07:22 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22