array of N pointers

This is a discussion on array of N pointers within the C Programming forums, part of the General Programming Boards category; Hello, I need some help on this: Isn't int *array[3]; the same thing as int *array = (int*) malloc(3*sizeof(int*)); ?? ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Location
    Portugal
    Posts
    18

    array of N pointers

    Hello, I need some help on this:

    Isn't

    int *array[3];

    the same thing as

    int *array = (int*) malloc(3*sizeof(int*)); ??

    I used the first declaration to print the integers: *array[i] and it worked.

    When I used the second one for N pointers, I get an error about invalid argument type of 'unary*' (have 'int').

    Thank you.

    Edit:

    Maybe pasting the whole code it will help:

    Code:
    #include <pthread.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <sys/types.h>
    #include <unistd.h>
    #include <errno.h>
    
    void *myfunc(void *arg)
    {
    	int *x = (int*) arg;
    	printf("O meu pid  %u, o meu tid  %u e recebi arg=%d \n", (unsigned) getpid(), (unsigned) pthread_self(), *x);	               
    	return (void*) x;
    }
    
    int main (int argc, char* argv[])
    {
    	if (argc != 2)
    	{
    		perror("Falta de argumentos!\n");
    		printf("Erro %d\n", errno);
    		_exit(1);
    	}	 
    	int n = atoi(argv[1]), st, i; 
    	pthread_t *tids = (pthread_t*) malloc(n*sizeof(pthread_t));
    	int *retval = (int*) malloc(n*sizeof(int*));
    	for(i=1; i<=n; i++)
    	{	 
    		st= pthread_create(&tids[i-1], NULL, myfunc, (void*) &i);
    		if (st != 0) 
    		{ 
    			perror("Erro em pthread_create()\n"); 
    			printf("Erro %d\n", errno);
    			_exit(2); 
    		}
    	}
    	
    	for (i=1; i<=n; i++)
    	{
    		st = pthread_join(tids[i-1], (void**) &retval[i-1]); 
    		if (st != 0) 
    		{ 
    			perror ("Erro em pthread_join()\n"); 
    			printf("Erro %d\n", errno);
    			_exit(3);
    		}
    		printf("Thread %u devolveu %d\n", (unsigned) tids[i-1], *retval[i-1]);
    	}
    	_exit(0);
    }
    Last edited by Lima; 06-06-2009 at 11:24 AM.

  2. #2
    Registered User
    Join Date
    Jun 2009
    Posts
    452
    int *array = (int*) malloc(3*sizeof(int*)); should be


    int *array = (int*) malloc(3*sizeof(int));

    You want three integer cells, not three integer pointer cells. I think.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,711
    Quote Originally Posted by CLimaPT
    Isn't

    int *array[3];

    the same thing as

    int *array = (int*) malloc(3*sizeof(int*)); ??
    No, they are not the same. The former declares an array of 3 pointers to int; the latter declares a pointer to int.

    If you want a dynamic array of 3 pointers to int, you should write:
    Code:
    int **array = malloc(3 * sizeof(*array));
    Quote Originally Posted by CLimaPT
    When I used the second one for N pointers, I get an error about invalid argument type of 'unary*' (have 'int').
    What is the exact error message?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Mar 2007
    Location
    Portugal
    Posts
    18
    I get this:

    f3exercicio1.c: In function ‘main’:
    f3exercicio1.c:36: error: invalid type argument of ‘unary *’ (have ‘int’)

    Oh, I think I get it, thank you once again. No cast needed by the way?
    Last edited by Lima; 06-06-2009 at 11:47 AM.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,711
    Quote Originally Posted by CLimaPT
    f3exercicio1.c: In function ‘main’:
    f3exercicio1.c:36: error: invalid type argument of ‘unary *’ (have ‘int’)
    Line 36 appears to be a blank line... so which line is line 36?

    By the way, there is no need to cast a pointer to be a pointer to void. The conversion will happen implicitly. There is also no need to cast a void* to be the desired pointer type unless you are trying to be compatible with C++.

    You should also get used to the idiom of starting your loop counters from 0 instead of 1 when you are using the loop counters to index an array.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Mar 2007
    Location
    Portugal
    Posts
    18
    This line:

    printf("Thread %u devolveu %d\n", (unsigned) tids[i-1], *retval[i-1]);

    It was line 46, my bad, pasted the other program output.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,711
    Right. Since retval is a pointer to int, retval[i-1] is an int, so *retval[i-1] does not make sense. Perhaps you mean to make retval a pointer to pointer to int, as I demonstrated in post #3.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Mar 2007
    Location
    Portugal
    Posts
    18
    Yeah, I see my mistake now. :P

    Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Returning an Array of Pointers to Objects
    By randomalias in forum C++ Programming
    Replies: 4
    Last Post: 04-29-2006, 02:45 PM
  2. two-dimensional dynamic array of pointers to classes
    By Timo002 in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 06:18 AM
  3. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 05:58 PM
  4. array of pointers to struct array
    By eth0 in forum C++ Programming
    Replies: 1
    Last Post: 01-08-2004, 05:43 PM
  5. array of pointers to structs
    By stumon in forum C Programming
    Replies: 7
    Last Post: 03-24-2003, 06:13 AM

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