typecasting void *pointers

This is a discussion on typecasting void *pointers within the C Programming forums, part of the General Programming Boards category; Code: int a[2] = {0,1}; void *b; b = (int *)&a; b++; printf("a's address: %p\n", b); Hi all, i typecasted ...

  1. #1
    Registered User
    Join Date
    Feb 2004
    Posts
    42

    typecasting void *pointers

    Code:
    	int a[2] = {0,1};
    	void *b;
    
    	b = (int *)&a;
    	b++;
    
    	printf("a's address: %p\n", b);
    Hi all, i typecasted the void pointer and try to increment (b++) it to point to the next address but there is this error: "error C2036: 'void *' : unknown size".

    Can someone give me some advise on this problem.

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,709
    You typecasted a not b. This is correct.
    Code:
    (int *) b = &a;

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No actually neither one of you are right. You never have to typecast assignments to void pointers. What it is complaining about is you incrementing b, not assigning it a value.
    Code:
    ((int*)b)++;
    gcc gives a warning about lvalue casts being depreciated, but in this case, it's wrong.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    I'm sure someone will be along with the right answer eventually.

    Hint: consider what sizeof(void) means.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Feb 2004
    Posts
    42
    Code:
    int a[2] = {0,1};
    void *b;
    
    b = &a;
    ((int*)b)++;
    printf("a's address: %p\n", b);
    Thanks guys, i know that the problem is about the incrementing b , and i tried
    Code:
    ((int*)b)++;
    before but it still couldn't work: "error C2105: '++' needs l-value".

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Salem
    I'm sure someone will be along with the right answer eventually.

    Hint: consider what sizeof(void) means.
    I'll assume you're not talking about my post. If so, what's the correct way? This works just fine, according to a pedantic-ansi-GCC.
    Code:
    #include<stdio.h>
    int main( void )
    {
    	int a[] = { 1, 2 };
    	void* b;
    
    	b = a;
    	((int*)b)++;
    	printf( "%d\n", *(int*)b );
    	return 0;
    }
    
    gcc -o inc inc.c -Wall -pedantic -ansi
    inc.c: In function `main':
    inc.c:8: warning: use of cast expressions as lvalues is deprecated
    ./inc
    2
    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    > "error C2036: 'void *' : unknown size".
    char *p ; p++;
    This advances p through memory by sizeof(char) bytes

    int *p; p++;
    This advances p through memory by sizeof(int) bytes

    Now, given that void has no size, what do you expect
    void *p ; p++
    to do?
    void has no size (that's what the error message says), so the compiler doesn't know what to add.

    > b = &a;
    Do you know the difference between this, and say
    b = a;
    b = &a[0];

    I bet you tried this somewhere along the line, and got yet another error message.
    int *b;
    b = &a; // wrong
    But rather than fixing it, you reached for void* as some magic cure-all.

    How about
    Code:
    int a[2] = {0,1};
    int *b;
    b = a;
    b++;
    printf("a's address: %p\n", b);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User
    Join Date
    Feb 2004
    Posts
    42
    Hi Salem, appreciated the help. I was actually trying out the use of void pointers. You see, i actually need to process a array in terms of int then char:

    Code:
    int a[2] = {0,1};
    void *b;
    
    b = a;
    // do some processing
    ((int*)b)++;
    
    
    ((char*)b)++;
    // do some processing
    
    ((char*)b)++;
    // do some processing
    
    ((char*)b)++;
    // do some processing
    
    ((char*)b)++;
    // do some processing

    As for
    int *b;
    int a[2] = {1,0};
    >b = a;
    >b = &a[0];

    I believe b is pointing to the 1st int member in a's array.

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,311
    In that case, Eavan, I suggest it is unnecessary to mess around with a void pointer;
    Code:
    int a[2] = {0,1};
    char *b;
    
    b = a;
    // do some processing
    
    *((int *)b) = 42;    //    Example;  sets first int to 42
    
    
    b += sizeof(int);
    
    ++b;
    // do some processing
    
    ++b;
    // do some processing
    
    ++b;
    // do some processing
    
    ++b;
    // do some processing
    Makes it a bit more obvious what you're doing, without having to jump through as many hoops with conversions.

  10. #10
    Registered User
    Join Date
    Jul 2006
    Posts
    7
    How about
    b = (&a) [+1]

  11. #11
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Quote Originally Posted by puppy
    How about
    b = (&a) [+1]
    I don't get it.
    Sent from my iPad®

  12. #12
    Registered User
    Join Date
    Jul 2006
    Posts
    7
    Sorry ...

    Your original program compiles fine (gcc version 4.1.0)
    I have taken the liberty to slightly modify your prog to
    Code:
    #include <stdio.h>
    
    int main()
    {
    
    int a[2] = {0,1};
    void *b;
    
            b = (int *)&a;
            b++;
    
            printf("a's address: %p\n", &a);
            printf("b's address: %p\n", b);
    }
    the o/p is
    a's address: 0xbfdf66a8
    b's address: 0xbfdf66a9
    so the address has been incremented by 1 byte ...(in this case from 8 to 9)

  13. #13
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    > Your original program compiles fine (gcc version 4.1.0)
    Apparently, you need to add more compiler options to prevent you relying on all sorts of compiler extensions
    Code:
    $ gcc bar.c
    $ gcc -W -Wall -ansi -pedantic -O2 bar.c
    bar.c: In function ‘main’:
    bar.c:10: warning: wrong type argument to increment
    bar.c:12: warning: format ‘%p’ expects type ‘void *’, but argument 2 has type ‘int (*)[1u]’
    bar.c:14: warning: control reaches end of non-void function
    Yes, it does "compile fine" if you have a lazy approach to diagnosing potential problems,
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  14. #14
    Registered User
    Join Date
    Jul 2006
    Posts
    7
    sorry ..
    I used make bar
    therefor no errors ?

  15. #15
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by puppy
    sorry ..
    I used make bar
    therefor no errors ?
    Possibly. But makefiles are just plain text and can be adjusted.
    Kurt

Page 1 of 2 12 LastLast
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. game window rejected painting !
    By black in forum Windows Programming
    Replies: 4
    Last Post: 03-27-2007, 01:10 AM
  3. ChangeDisplaySettings - Blank?
    By Tonto in forum Windows Programming
    Replies: 13
    Last Post: 12-26-2006, 03:17 PM
  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

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