Storing array in a void pointer

This is a discussion on Storing array in a void pointer within the C Programming forums, part of the General Programming Boards category; Hello, I've been programming in C for just under 3 days, and I have next to no idea what I'm ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    5

    Storing array in a void pointer

    Hello, I've been programming in C for just under 3 days, and I have next to no idea what I'm doing. I'm writing a method which gets passed (among other things) void *buf.
    I have a character array which i have declared

    char *temp[len];

    where len is an unsigned integer. How do I set it up so that buf points to temp?

    is it as simple as writing

    buf=temp; ???

    This was the first thing I tried, and I got no errors, but when I run the program to call the method, which prints the contents of buf, I get no output, which suggests to me this is wrong.

    Then I though that I would try something like

    temp=(char *)buf;

    but I get
    "incompatible types when assigning to type ‘char *[(unsigned int)(len)]’ from type ‘char *’"

    All I want to do is write an array wherever buf points to! Surely it shouldn't be complicated...please help me!

  2. #2
    Registered User
    Join Date
    Aug 2010
    Posts
    23
    first of all you've declared the character array incorrectly..

    Code:
    char *temp[len];
    should be

    Code:
    char temp[len];
    what you've declared is an array of pointers to characters... also, the "len" used above cannot be a variable since arrays are allocated memory statically, i.e. when the program compiles. so "len" can only be an integer constant.

    now if you have the void pointer as..

    Code:
    void *buf;
    
    buf = (char *)temp;
    should work fine....

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    5
    Ok but len is a variable, so I need to declare as

    Code:
    	char* temp;
    	temp=(char*) malloc(len);
    or something similar? In which case I need to manually copy each element from this to buf, yes?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    Quote Originally Posted by formicas
    Ok but len is a variable, so I need to declare as (...) or something similar?
    Yes, but you do not need to cast the return value of malloc. You could use a variable length array if you were compiling with respect to the 1999 edition of the C standard, or if you used a compiler extension, but it depends on the context and requirements.

    Quote Originally Posted by formicas
    In which case I need to manually copy each element from this to buf, yes?
    buf is just a pointer. You would just write buf = temp;
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Sep 2010
    Posts
    5
    But then I can't free the memory I just allocated, can I? I know practically it won't hurt, but my lecturer hates doing things he considers "unsafe".

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    Quote Originally Posted by formicas
    But then I can't free the memory I just allocated, can I?
    You can still free(temp) after that function returns back to the caller. Actually, you do not even need to have a buf variable: you can pass temp as an argument to that function directly.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Sep 2010
    Posts
    5
    Yeah, only problem is I'm not allowed to edit the code that calls the function! Anyway, with your advice I have managed to slap together a solution. Cheers!

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    You might want to post the function. There is a serious possibility that you have written code that is horribly wrong.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Sep 2010
    Posts
    5
    Nah, there is too much in too many files. It's not fair to ask you to trawl through it. But thanks for your concern.

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,170
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 08-16-2010, 10:00 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Replies: 16
    Last Post: 10-29-2006, 04:04 AM
  4. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 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

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