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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 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()` – Christian Gibbons Oct 02 '17 at 15:36
  • @Bathsheba Can you explain it in a detailed way. I couldn't understand it. – Aashish Loknath Panigrahi Oct 02 '17 at 15:36
  • @ChristianGibbons Is there any other way of returning values of an array without malloc ?? – Aashish Loknath Panigrahi Oct 02 '17 at 15:38
  • 1
    1) There is an illegal character before `int NumberArray[100];`. 2) `int NumberArray[100];` --> `int *NumberArray = malloc(100*sizeof(*NumberArray));` – BLUEPIXY Oct 02 '17 at 15:40
  • Yes, if you created the array in the calling function and passed a pointer to it as an argument to the called function, the changes you make to the array in the called function will will be visible from the calling function. – Christian Gibbons Oct 02 '17 at 15:40
  • Possible duplicate of [Returning an array using C](https://stackoverflow.com/questions/11656532/returning-an-array-using-c) – Andrew Henle Oct 02 '17 at 16:10
  • 1
    For the 100th time: thou shalt not return pointers to local variables from thy functions. – Jabberwocky Oct 02 '17 at 16:10
  • 2
    You have a couple of UTF-8 ["NO-BREAK SPACE"](http://www.fileformat.info/info/unicode/char/00a0/index.htm) 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. – Nisse Engström Oct 02 '17 at 20:43
  • 302 240 (octal) → 0xC2 0xA0 (hexadecimal) → UTF-8 sequence for Unicode code point U+00A0 ([NO-BREAK SPACE](https://www.charset.org/utf-8)). This character can be searched for (and replaced with a regular space character) using the regular expression `\x{00A0}` (in any modern text editor or IDE). – Peter Mortensen Apr 26 '23 at 00:54
  • This is a ***very*** common error when copying code from web pages, [PDF](https://en.wikipedia.org/wiki/Portable_Document_Format) documents, through chat (e.g. [Skype Chat](https://en.wikipedia.org/wiki/Features_of_Skype#Skype_chat) or [Facebook Messenger](https://en.wikipedia.org/wiki/Facebook_Messenger)), etc. The canonical question is *[Compilation error: stray ‘\302’ in program, etc.](https://stackoverflow.com/questions/19198332)*. – Peter Mortensen Apr 26 '23 at 00:55
  • Why are the answers ignoring the stray problem (caused by the weird characters, most likely all NO-BREAK SPACE)? It will not compile before those characters have been replaced. – Peter Mortensen Apr 26 '23 at 00:59

3 Answers3

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);

odin
  • 392
  • 1
  • 3
  • 11
  • "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. – Support Ukraine Oct 02 '17 at 15:39
  • 1
    BTW - don't cast malloc – Support Ukraine Oct 02 '17 at 15:40
  • @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. – odin Oct 02 '17 at 15:42
  • 1
    Casting malloc is usually frowned upon in C. C++ will complain about it, but that is a different language. – Christian Gibbons Oct 02 '17 at 15:43
  • 1
    @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. – Support Ukraine Oct 02 '17 at 15:45
  • Point taken on the malloc, will edit that. TIL for automatic allocation, I have always heard statically allocated, but I guess that is wrong per the GNU standard. – odin Oct 02 '17 at 15:49
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.

Christian Gibbons
  • 4,272
  • 1
  • 16
  • 29
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Chris Dodd
  • 119,907
  • 13
  • 134
  • 226
  • \302\240 is the UTF-8 sequence for Unicode code point U+00A0 ([NO-BREAK SPACE](https://www.charset.org/utf-8)). It can be searched for directly (and replaced) by the regular expression `\x{00A0}` in any modern text editor (say, [Geany](https://pmortensen.eu/world2/2020/03/29/using-geany/) or [Visual Studio Code](https://en.wikipedia.org/wiki/Visual_Studio_Code)). There are two instances in the source code as posted. – Peter Mortensen Apr 27 '23 at 12:43
  • Note: The notation is different in Visual Studio Code (and probably others): `\u00A0` (instead of `\x{00A0}`) – Peter Mortensen Apr 27 '23 at 12:44