Thread: Typedef array variable

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    250

    Typedef array variable

    Whilst performing a 'typedef int triple[3]', I get results that are confusing to me. The type of the stack variable is indeed an int *[3]. However, 's' is not an int[3], but an int *[3] also. Furthermore, when dereferencing stack[5], the resulting lvalue is an int[3], as I would expect, but when assigning this to 's', I get the pointer to this triple, presumably because s is defined as an int *[3], not an int[3].

    My questions are: why is 's' defined as an int *[3], instead of an int[3] and why does assigning an int[3] to 's' result in the assignment of the address of this particular int[3]?

    The code has been compiled with gcc-4.1.2.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef int triple[3];
    
    int
    main()
    {
    	triple *stack;
    	triple s;
    	int i, j;
    
    	printf("sizeof(stack): %i\n", sizeof(stack));
    
    	stack = (triple *) malloc(10*sizeof(stack));
    	if(stack == NULL)
    		return 1;
    
    	printf("sizeof(stack) after malloc: %i\n", sizeof(stack));
    
    	for(i = 0; i < 10; i++)
    		for(j = 0; j < 3; j++)
    			stack[i][j] = i*3 + j;
    
    	s = stack[5];
    
    	printf("s[0], s[1], s[2]: %i, %i, %i\n", s[0], s[1], s[2]);
    	
    	return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > printf("sizeof(stack): %i\n", sizeof(stack));
    1. You should probably use %lu to print the result which sizeof evaluates to
    2. This only prints how big the pointer is (probably 4 on most machines)

    > stack = (triple *) malloc(10*sizeof(stack));
    1. Don't cast malloc in C
    2. You've used the size of the pointer, not what it points to.
    The idiom to use is
    p = malloc ( num * sizeof *p );

    > s = stack[5];
    Remember that all you have done is create a typedef for the array. So the rule that you can't assign arrays still stands.

    In effect, you have these objects in your assignment.
    int s[3];
    int stack[10][3];

    If you really want to be able to assign these, then you need to hide your array inside a struct, which is something you can assign.
    Eg.
    Code:
    typedef struct triple {
       int triple[3];
    } triple;
    But remember that you'll end up with some dots in all your variable references.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. typedef void * variable;
    By cblix in forum C Programming
    Replies: 3
    Last Post: 11-06-2005, 07:48 PM
  2. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  3. Variable Allocation in a simple operating system
    By awkeller in forum C Programming
    Replies: 1
    Last Post: 12-08-2001, 02:26 PM
  4. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM
  5. making an array name a variable
    By Zaarin in forum C++ Programming
    Replies: 5
    Last Post: 09-02-2001, 06:17 AM