Comparing Pointer types in C

This is a discussion on Comparing Pointer types in C within the C Programming forums, part of the General Programming Boards category; Hi, I have this program in C, and have commented each line as per my understanding. At the end, I ...

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    10

    Question Comparing Pointer types in C

    Hi,

    I have this program in C, and have commented each line as per my understanding. At the end, I have some questions which I havent been able to understand. Any help would be appreciated.

    Code:
    
    #include <stdio.h>
    
    #define Size1 (4)
    #define Size2 (3)
    #define Size3 (5)
    
    int main() {
      int* a1 = malloc(4 * sizeof(int)); /*16 bytes*/
      int* a2 = malloc(3 * sizeof(int)); /*12 bytes*/
      int* a3 = malloc(5 * sizeof(int)); /*20 bytes/*
      a1[0] = 1; a1[1] = 2; a1[2] = 3; a1[3] = 4; /*filling array a1*/
      a2[0] = 9; a2[1] = 8; a2[3] = 7; /*filling array a2*/
      a3[0] = -1; a3[1] = -2; a3[2] = -3; a3[3] = -4; a3[4] = -5; /*filling array a3*/
    
      int sizes[ ] = {Size1, Size2, Size3, 0}; /*local array of sizes*/
    
      int** ptr2ptr = malloc(sizeof(int*) * 4); /*array of 16 bytes*/
      ptr2ptr[0] = a1; /*storing the pointer to a1 in first element*/
      ptr2ptr[1] = a2; /*storing the pointer to a2 in second element*/
      ptr2ptr[2] = a3; /*storing the pointer to 03 in second element*/
      ptr2ptr[3] = 0; /* this is the null pointer for termination*/
    
      int i = 0;
      /*here we are looping until we dont reach the null terminator which ptr2ptr[3]*/
      while (ptr2ptr[i]) {
        int j;
        for (j = 0; j < sizes[i]; j++) /*for each loop print out the contents of the pointer and increment i*/
          printf("%i ", ptr2ptr[i][j]);
        printf("\n");
        i++; /*increment i*/
      }
      return 0;
    Questions:

    /*
    Q1: Are a1, a2, and a3 of the same type?

    Ans. I compared a1 and a2 using == it said not equal. Why? Is there a way I can compare these pointers?

    Q2.Are a1 and ptr2ptr of the same type?

    Ans: I tried to compare them and got the following: "warning: comparison of distinct pointer types lack a cast". Why?

    Q3. If a1, a2, and a3 are considered to be arrays, are they of the same size?

    Ans: I ran a sizeof() on them, but I think that is not right as here by size we mean "Length", so how do I do this?

    Q4. If ptr2ptr is considered to be an array, is it the same size as any of a1, a2, or a3?

    Ans: I guess if I know Q3 I will know Q4.


    */

    Thanks for the assistance.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    == compares values, it does not compare types. You should be able to figure out the types by looking at the declarations.

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    10
    Quote Originally Posted by tabstop View Post
    == compares values, it does not compare types. You should be able to figure out the types by looking at the declarations.
    So, is it safe to say that a1, a2 and a3 are of the same type as they are on the heap?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by ladesidude View Post
    So, is it safe to say that a1, a2 and a3 are of the same type as they are on the heap?
    Heap has nothing to do with it. a1 is declared as an int*. a2 is declared as an int*. a3 is declared as an int*. ptr2ptr is declared as an int**.

  5. #5
    Registered User
    Join Date
    Jun 2008
    Posts
    10
    Quote Originally Posted by tabstop View Post
    Heap has nothing to do with it. a1 is declared as an int*. a2 is declared as an int*. a3 is declared as an int*. ptr2ptr is declared as an int**.
    To really dumb this down, a1, a2 and a3 are of the same type while int2ptr is different from a1?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Yes.

  7. #7
    Registered User
    Join Date
    Jun 2008
    Posts
    10
    Quote Originally Posted by tabstop View Post
    Yes.

    How can I prove that a1,a2 and a3 are of the same type?

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by ladesidude View Post
    How can I prove that a1,a2 and a3 are of the same type?
    I don't know what you mean by "prove". I know what I mean by prove, and I already proved that a1, a2, and a3 are the same type above. There is no way to programmatically get the types of variables as the program is running.

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >How can I prove that a1,a2 and a3 are of the same type?
    By looking at how they are declared. As a simple example, if a and b are declared as int, they are of the same type. If a is declared as an int, and b as a double, they are of different types.

    If you are asking is it possible to prove a and b are of the same pointer type without looking at the declaration, then you can when you compile the program, but not when you run the program. If a and b aren't of the same pointer type, then following line will generate a warning something like "comparing incompatible pointer types".
    Code:
    if (a == b)

  10. #10
    Registered User
    Join Date
    Jun 2008
    Posts
    10
    Quote Originally Posted by swoopy View Post
    >How can I prove that a1,a2 and a3 are of the same type?
    By looking at how they are declared. As a simple example, if a and b are declared as int, they are of the same type. If a is declared as an int, and b as a double, they are of different types.

    If you are asking is it possible to prove a and b are of the same pointer type without looking at the declaration, then you can when you compile the program, but not when you run the program. If a and b aren't of the same pointer type, then following line will generate a warning something like "comparing incompatible pointer types".
    Code:
    if (a == b)
    I ran this code:
    Code:
     if(a1 == a2)
    	printf("Equal %i",a1==a2);
      else
    	printf("Not Equal %i\n", a1 != a2);
    and it came back saying not equal, but this doesnt prove that they are of the same type.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by ladesidude View Post
    I ran this code:
    Code:
     if(a1 == a2)
    	printf("Equal %i",a1==a2);
      else
    	printf("Not Equal %i\n", a1 != a2);
    and it came back saying not equal, but this doesnt prove that they are of the same type.
    We know that. Try going back to the start, and reading our responses this time. There is a large large difference between what kind of variable a1 and a2 are, and the actual value stored in them.

  12. #12
    Registered User
    Join Date
    Jun 2008
    Posts
    10
    Quote Originally Posted by tabstop View Post
    We know that. Try going back to the start, and reading our responses this time. There is a large large difference between what kind of variable a1 and a2 are, and the actual value stored in them.
    OK, so if they are pointers of type int, they are of the same type, but because they have different values, the == will fail. Am I correct in assuming this?

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by ladesidude View Post
    OK, so if they are pointers of type int, they are of the same type, but because they have different values, the == will fail. Am I correct in assuming this?
    You shouldn't be assuming this, you should be reading this out of your code.

  14. #14
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >and it came back saying not equal, but this doesnt prove that they are of the same type.
    Right, but since the compiler didn't generate any warning for that line, it proves a1 and a2 are of the same pointer type.

    Type means what type is the variable (int, double, char, pointer to int, pointer to pointer to int, etc). But pointers hold addresses, and two pointers of the same type will very likely contain different addresses. So they won't compare equal, unless you assigned them same address.

  15. #15
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    602
    I do believe the differences between what a pointer is and how to declare its type has been addressed but I believe that there is a mis understanding of the usage of pointers by the OP.

    When you need to make use of a pointer, an address, as computers would rather work with an address than a variable for efficiency means, then you must tell the compiler how much memory to set aside for a given variable that you wish to store for this pointer.

    An int is 4 bytes for an example, so when you declare a pointer to an int, you are advising the compiler how much memory to set aside for a variable. When you later set aside more memory by malloc or realloc then you are again instructing the compiler that you wish to set aside memory to be used by that pointer. So a pointer type has nothing to do with the value it "points" to. When you compare pointers a1==a2, you are only comparing the address that is storing a potential variable not the value.

    You can not really guarantee type during run time, type is defined at compile time and is a way to instruct the compiler on the programmer intentions.

    You can mess around with sizeof() and make guesses as to their types by the memory chunks being used, but that is a shot in the dark at best.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why does C need pointer conversion
    By password636 in forum C Programming
    Replies: 2
    Last Post: 04-10-2009, 07:33 AM
  2. Ban pointers or references on classes?
    By Elysia in forum C++ Programming
    Replies: 89
    Last Post: 10-30-2007, 03:20 AM
  3. Question About Pointer To Pointer
    By BlitzPackage in forum C++ Programming
    Replies: 2
    Last Post: 09-19-2005, 10:19 PM
  4. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 01:19 AM
  5. incompatible pointer types
    By mart_man00 in forum C Programming
    Replies: 3
    Last Post: 04-20-2003, 08:32 PM

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