can't do sizeof(pointer)

This is a discussion on can't do sizeof(pointer) within the C Programming forums, part of the General Programming Boards category; Code: #define aVar sizeof(char) #define bVar sizeof(short) #define cVar sizeof(int) #define dVar sizeof(long) #define eVar sizeof(float) #define fVar sizeof(double) #define ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    10

    can't do sizeof(pointer)

    Code:
    #define aVar sizeof(char)
    #define bVar sizeof(short)
    #define cVar sizeof(int)
    #define dVar sizeof(long)
    #define eVar sizeof(float)
    #define fVar sizeof(double)
    #define gVar sizeof(pointer)
    #define hVar sizeof(struct student)
    
    int main(int argc, char *argv[])
    {
    char response;
    	do {
            printf("On this machine the number of bytes in a char is: %d\n", aVar);
            printf("On this machine the number of bytes in a short is: %d\n", bVar);
    	printf("On this machine the number of bytes in a int is: %d\n", cVar);
    	printf("On this machine the number of bytes in a long is: %d\n", dVar);
    	printf("On this machine the number of bytes in a float is: %d\n", eVar);
    	printf("On this machine the number of bytes in a double is: %d\n", fVar);
    	printf("On this machine the number of bytes in a pointer is: %d\n", gVar);
    	printf("On this machine the number of bytes in a struct student is: %d\n", hVar);
    
    printf("Would you like to see the information again?");
    
    scanf("%c", &response);
    switch (response) {
            case 'y':
    	        scanf("%c", &response);
    	        break;
    
    	default:
    		printf("Goodbye");
    		return 0;
    		break;
    		}
    }while(response = 'y');
    }
    Here, the sizeof() are displayed. All of them work except sizeof(pointer). I get "undeclared identifier 'pointer'" error for the printf line. How can I fix this?
    And while we're here, is my code good or bad? Is there any way to shrink or make it efficient? Thanks

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Code:
    #define gVar sizeof(pointer)
    well, there is nothing called 'pointer' datatype in 'C'. in your case its not a datatype, sturctures, unions or its not a varibale of any datatype as well. its just a word which is not defined. thats why u get the error.

    to be more meaningfull do it like this
    Code:
    #include<stdio.h>
    
    int main()
    {
        int *p;
        
        printf("%d",sizeof(p));  //as p is a pointer of type int it has to show 4 bytes
        getchar();
    }
    /*myoutput
    4
    */
    ssharish2005
    Last edited by ssharish2005; 10-16-2005 at 07:18 PM.

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Code:
    #define gVar sizeof(pointer)
    so what type is the identifer "pointer" because it sure isn't a basic type. Its time for you to go back and read up on pointers and the syntax for them.

    And while we're here, is my code good or bad? Is there any way to shrink or make it efficient? Thanks
    Bad in my book, overuse of #define that do nothing but obfuscate the code. bVar isn't a variable which the name implies. Like wise for the others.

    You could also norrow down the number of calls but using only one printf but I'm not sure how much good that would do.

    }while(response = 'y');
    That is assignment not comparison

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    10
    thanks, guys. I just started learning C a few days ago, and I think I'm way in over my head. It took me hours to write that and it turned into a bloated pos.

  5. #5
    Registered User TactX's Avatar
    Join Date
    Oct 2005
    Location
    Germany.Stuttgart
    Posts
    65
    And btw. sizeof(char) is always 1. On any system/implementation.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    42
    And btw. sizeof(char) is always 1. On any system/implementation
    I'm not arguing that this is true, but I'm curious if this causes problems. Aren't there systems that natively use 2 bytes for a character? If so wouldn't this cause problems when mallocing characters?

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by the pooper
    I'm not arguing that this is true, but I'm curious if this causes problems. Aren't there systems that natively use 2 bytes for a character? If so wouldn't this cause problems when mallocing characters?
    I'd imagine your intended meaning of "2 bytes for a character" is a 16-bit character. Such a system would likely have a CHAR_BIT of 16 and life merrily chugs along.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    The standard requires that sizeof(char) be one, all other datatypes use multiples of that value. Now this doesn't mean that all characters take up the same amount of bits. CHAR_BIT determines the number of bits per byte. And sizeof() returns the number of bytes.

    Now of course this is only for the base char type and not the wide char type needed for such thing as unicode.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    42
    I see. I made the mistake of assuming that a byte is always 8 bits.

Popular pages Recent additions subscribe to a feed

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