Trouble with pointers

This is a discussion on Trouble with pointers within the C Programming forums, part of the General Programming Boards category; Hey guys, just having some trouble with a pretty simple program and I can't for the life of me understand ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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, 12:19 AM
  2. Replies: 4
    Last Post: 12-10-2006, 06:08 PM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  4. trouble with pointers HELP MEEEE???
    By drdodirty2002 in forum C++ Programming
    Replies: 1
    Last Post: 03-09-2004, 11:39 PM
  5. pointers, functions, parameters
    By sballew in forum C Programming
    Replies: 3
    Last Post: 11-11-2001, 09:33 PM

Tags for this Thread


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