Thread: Trouble with pointers

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    4

    Trouble with pointers

    Hey guys, just having some trouble with a pretty simple program and I can't for the life of me understand where the problem lies. Maybe someone can give me a hand?

    Code:
    int ** createArray(int rows, int cols)
    {
    	int i;
    	int **ptr_9 = (int **)malloc(rows * sizeof(int *));
    
    	if (ptr_9 == NULL) 
    	{
        		printf("Error");
        		return 1;
      	}
    
    	for (i = 0 ; i < rows ; i++)
    	{
        		ptr_9[i] = (int *)malloc(cols * sizeof(int));
      	}
    	return ptr_9;
    }
    as far as I can see this should be setting aside the memory for ptr_9 which is then linked to a pointer in my main(). Now it is in fact doing this however cygwin is throwing me the "return makes pointer from integer without a cast" warning. Can someone tell me where the problem lies? I essentially have the same code operating elsewhere with no problems. Thanks in advance!

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    The warning comes from the "return 1;" line. "1" is an integer, which will be implicitly casted to an "int**". You should probably return "NULL".

    On a side note, you (correctly) make sure that "malloc" was successful for the rows. However, for each column you should check that it is successful there, too.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    See our FAQ on casting malloc.
    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.

  4. #4
    Registered User
    Join Date
    Jan 2010
    Posts
    4
    Quote Originally Posted by nadroj View Post
    The warning comes from the "return 1;" line. "1" is an integer, which will be implicitly casted to an "int**". You should probably return "NULL".

    On a side note, you (correctly) make sure that "malloc" was successful for the rows. However, for each column you should check that it is successful there, too.
    haha! Oh man, brutal! Thanks so much I didn't even consider that! And yeah I'll definitely add in the check for the columns.

    Great help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble with pointers..
    By elfjuice in forum C Programming
    Replies: 7
    Last Post: 11-25-2007, 01:19 AM
  2. Replies: 4
    Last Post: 12-10-2006, 07:08 PM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. trouble with pointers HELP MEEEE???
    By drdodirty2002 in forum C++ Programming
    Replies: 1
    Last Post: 03-10-2004, 12:39 AM
  5. pointers, functions, parameters
    By sballew in forum C Programming
    Replies: 3
    Last Post: 11-11-2001, 10:33 PM

Tags for this Thread