Thread: New to c, help with (void *)

  1. #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!

  2. #2
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    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.

  3. #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 *?

  4. #4
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    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.

  5. #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

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Replies: 3
    Last Post: 05-13-2007, 08:55 AM
  3. game window rejected painting !
    By black in forum Windows Programming
    Replies: 4
    Last Post: 03-27-2007, 01:10 AM
  4. msvc just ate one of my source files
    By Eber Kain in forum C++ Programming
    Replies: 6
    Last Post: 07-01-2004, 05:40 AM
  5. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM

Tags for this Thread