Returning an Array

This is a discussion on Returning an Array within the C Programming forums, part of the General Programming Boards category; Hi i undestand that in C you can not return an array, but a pointer to an array. But i ...

  1. #1
    Registered User mmarab's Avatar
    Join Date
    Jul 2007
    Posts
    30

    Question Returning an Array

    Hi i undestand that in C you can not return an array, but a pointer to an array. But i am having trouble doing this, this is what i have done do far:

    Code:
    char * numberCon()
    {
       char numbers[10]
        for()
       {
         //adds numbers into the array
       }
       
      return numbers;
    }
    
    int main()
    {
      char * pt = numberCon();
      int i =0;
     for(;i<10;i++)
     {
       printf("%d", &pt);
       pt++;
     }
    }
    doing this the compiler gives me the following warning: "Suspicious pointer conversion in function numberCon" and also the incorrect values are printed out.

    Any help would be great.

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Your numbers variable only has scope local to the numberCon function. When the function returns whatever 'pt' has a hold of will be garbage, hence the incorrect values.

    Either make numbers static or dynamically allocate memory and return a pointer to that, remembering to free it when done.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    151
    The problem of yours is simple. You just misunderstood the difference between and numbers[10] defined string and a char * defined string. s points and adress right. But you can not assign that to an empty string adress,coz numbers keeps an adress that points a 10 charactered string.So you should change you numbercon function like this:


    return &numbers[0];

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by ozumsafa View Post
    The problem of yours is simple. You just misunderstood the difference between and numbers[10] defined string and a char * defined string. s points and adress right. But you can not assign that to an empty string adress,coz numbers keeps an adress that points a 10 charactered string.So you should change you numbercon function like this:


    return &numbers[0];
    No you shouldn't, that won't make a difference. See Ken Fitlike's post.

  5. #5
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    There's also the classic solution, that's it, passing "an array" to your numberCon fonction, which could looks like this

    Code:
    char *numberCon(char *array, int arraySize)
    {
    	int i;
    
    	for (i = 0; i < arraySize; i++)
    	{
    		//...
    	}
    
    	return array;
    }
    
    
    int main()
    {
    	char numbers[10];
    	char *pt;
    
    	pt = numberCon(numbers, 10);
    
    	//...
    
    	return 0;
    }
    Anyway.

  6. #6
    Registered User
    Join Date
    Jul 2007
    Posts
    151
    He is right. Forget about my post. That was an uncareful one anyway. I do not have the time right now . But I had a look again , the structure is in pain. First of all you shouldnt set an algorithm like that. You have also some operator errors. Based on what I saw in limited time. I will try to write more later.

  7. #7
    Registered User
    Join Date
    Jul 2007
    Posts
    151
    by operator I mean : &pt

  8. #8
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Posts
    311

    Redundant

    Quote Originally Posted by foxman View Post
    There's also the classic solution, that's it, passing "an array" to your numberCon fonction, which could looks like this

    Code:
    char *numberCon(char *array, int arraySize)
    {
    	int i;
    
    	for (i = 0; i < arraySize; i++)
    	{
    		//...
    	}
    
    	return array;
    }
    
    
    int main()
    {
    	char numbers[10];
    	char *pt;
    
    	pt = numberCon(numbers, 10);
    
    	//...
    
    	return 0;
    }
    Anyway.
    Arrays are passed "by reference" by default in C. There is no reason why you need to be returning the array from that function (or any function in C for that matter). The contents will be modified in the caller.

  9. #9
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    There is no reason why
    same reason as in strcpy... for the calling chain support for example
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  10. #10
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Posts
    311
    Quote Originally Posted by vart View Post
    same reason as in strcpy... for the calling chain support for example
    Indeed. I stand corrected.
    Last edited by MacNilly; 07-19-2007 at 05:42 AM.

  11. #11
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    Arrays are passed "by reference" by default in C. There is no reason why you need to be returning the array from that function (or any function in C for that matter). The contents will be modified in the caller.
    I know that. I just wanted my function to return something, it's free for you to use or not use the return value.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Parsing and returning array pointers?
    By thealmightyone in forum C Programming
    Replies: 26
    Last Post: 03-26-2009, 03:38 PM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. Replies: 6
    Last Post: 10-21-2003, 09:57 PM
  4. passsing and returning char array
    By Bine in forum C++ Programming
    Replies: 4
    Last Post: 02-01-2003, 08:08 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM

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