Pointer cast question

This is a discussion on Pointer cast question within the C Programming forums, part of the General Programming Boards category; I'm a C beginner (come from a Java and Ruby background) and I'm trying to learn C by completing some ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    7

    Pointer cast question

    I'm a C beginner (come from a Java and Ruby background) and I'm trying to learn C by completing some tutorials.

    One tutorial gives some sample code to complete and I'm struggling with one line in particular. The code contains a sort function with the following prototype:

    Code:
    void sort (int *data[], int n, comp_ptr compare, swap_ptr swap);
    which is called in main.c like so:

    Code:
    sort((int**) employees, no_employees, comp_employee, swap_employee);
    The thing that's confusing me is the first parameter. employees is defined like so:

    Code:
    Employee *employees[10];
    with Employee being defined as:

    Code:
    typedef struct emp_struct 
    { char name[100];
    	int	employee_no;
    	float salary, tax_to_date; 
    } Employee;
    It seems that employees, in the above function call, would represent a pointer to to the first element in its array (which would itself represent a pointer to an Employee). What I don't understand -- really at all -- is how the (int**) cast would convert employees into a pointer to the first element of a array of pointers to ints, as specified in the sort function prototype.

    It seems like this is meant to convert an array of pointers to ints to an array of pointers to Employees. Is that actually what's happening? What happens when a program points to something expecting an int but actually gets an Employee?

    Any assistance here would be much appreciated. Many thanks!

  2. #2
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    993
    What I don't understand -- really at all -- is how the (int**) cast would convert employees into a pointer to the first element of a array of pointers to ints, as specified in the sort function prototype.
    It wouldn't. The code is absolutely incorrect.

    What will possibly happen is that each entry of the array in sort() will, instead of pointing to an int, will point to an Employee. Of course the compiler thinks they're ints at this point, so when you try to access that int, it will access the first 4 bytes (or however large an int is) if the struct. That means the first few characters of the name will be interpreted as an int. This is not guaranteed, because the behavior of the code is undefined. It just might help explain some results you see.

    Whoever wrote this tutorial is ignorant about C. Casting in C is almost never the right thing to do, and it is not a magic conversion tool the way this tutorial seems to be implying.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. a pointer to a function question..
    By transgalactic2 in forum C Programming
    Replies: 17
    Last Post: 10-21-2008, 12:47 PM
  2. Question about pointer arithmetic and types
    By brooksbp in forum C Programming
    Replies: 4
    Last Post: 08-22-2008, 02:53 PM
  3. Ban pointers or references on classes?
    By Elysia in forum C++ Programming
    Replies: 89
    Last Post: 10-30-2007, 04:20 AM
  4. Simple pointer question
    By jayznz in forum C Programming
    Replies: 2
    Last Post: 04-05-2006, 12:36 AM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM

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