0

I have been trying to return back an array using the code below -

#include <stdio.h>

int* factor(int num);

int main(void)
{
    int num;
    printf("\nEnter a number: ");
    scanf("%d", &num);
    int* array;
    array = factor(num);
    for(int counter=0; counter<num; counter++)
    {
        printf("%d\n", array[counter]);
    }
}

int* factor(int num)
{
   int NumberArray[100];
   for(int i=0; i<num; i++)
   {
       NumberArray[i] = i;
   }
   return NumberArray;
}

And this has generated the following output -

gcc assignment3func.c -o assignment3func
assignment3func.c: In function ‘factor’:
assignment3func.c:19:1: error: stray ‘\302’ in program
    int NumberArray[100];
 ^
assignment3func.c:19:1: error: stray ‘\240’ in program
assignment3func.c:19:1: error: stray ‘\302’ in program
assignment3func.c:19:1: error: stray ‘\240’ in program
assignment3func.c:23:11: warning: function returns address of local variable [-Wreturn-local-addr]
    return NumberArray;
           ^

Please help me out. I couldn't understand the stray thing.

11
  • 1
    NumberArray is scoped within your function factor(). Once you leave that scope, it no longer exists and so you are returning a pointer to something that does not exist. You will need to put it on the heap if you want it to survive beyond the scope of the function with malloc() Commented Oct 2, 2017 at 15:36
  • @Bathsheba Can you explain it in a detailed way. I couldn't understand it. Commented Oct 2, 2017 at 15:36
  • 1
    1) There is an illegal character before int NumberArray[100];. 2) int NumberArray[100]; --> int *NumberArray = malloc(100*sizeof(*NumberArray)); Commented Oct 2, 2017 at 15:40
  • 1
    For the 100th time: thou shalt not return pointers to local variables from thy functions. Commented Oct 2, 2017 at 16:10
  • 2
    You have a couple of UTF-8 "NO-BREAK SPACE" characters (hex: c2 a0, octal: 302 240) at the location indicated by the error messages. Try to remove and replace them with regular space characters. Commented Oct 2, 2017 at 20:43

3 Answers 3

2

That array is declared with automatic storage duration, so when it goes out of scope, it is deallocated by your compiler. If you want to create an array that you can return, allocate it with dynamic memory.

int* NumberArray = malloc(sizeof(int)*100);

Sign up to request clarification or add additional context in comments.

6 Comments

"statically" is probably not the best word as it makes one think of a static array which is not the case. Maybe "locally" or "with automatic storage duration" would be better.
BTW - don't cast malloc
@4386427 While statically may sound confusing, that is the technically correct term as far as I know. I have casted malloc because C/C++ compilers often warn if you don't.
Casting malloc is usually frowned upon in C. C++ will complain about it, but that is a different language.
@odin 1) The correct term is "automatic storage duration" - it will never be statically 2) In C you should never cast malloc. In c++ you need to but you should never use malloc in c++. They are different languages.
|
1

In response to your question about how to return an array without using malloc:

#include <stdio.h>
void factor(int num, int *NumberArray); //pass a pointer to an array to the function
int main(void)
{
  int num;
  printf("\nEnter a number:");
  scanf("%d",&num);
  int array[100]; //create the array in the calling function
  factor(num, array);
  for(int counter=0;counter<num;counter++)
  {
      printf("%d\n",array[counter]);
  }
}
void factor(int num, int *NumberArray)
{
   for(int i=0; i<num; i++){
       NumberArray[i] = i;
   }
}

Here you create the array in the calling function and pass a pointer to it in the called function. The called function then operates on the array that is scoped in the calling function.

Comments

1

The "stray '\302'"/"stray '\240'" messages are about having extra non-printing (invisible) Unicode characters in your source code. These usually come about from copy-pasting code from some other source.

Specifically, the two-byte \302\240 sequence is a UTF-8 encoding for a non-breaking space character. Unfortunately, deleting these with your editor can be tricky as they are invisible! If you can put your editor into ASCII mode instead of Unicode, they may show up so you can delete them.

2 Comments

\302\240 is the UTF-8 sequence for Unicode code point U+00A0 (NO-BREAK SPACE). It can be searched for directly (and replaced) by the regular expression \x{00A0} in any modern text editor (say, Geany or Visual Studio Code). There are two instances in the source code as posted.
Note: The notation is different in Visual Studio Code (and probably others): \u00A0 (instead of \x{00A0})

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.