0

Basically what I'm doing is making a base program that I can build other programs off of, and I have a function (called input) that returns the characters collected from a user input. Right now, there's a 2 errors saying "return makes integer without a cast", and "function returns address of local variable". What I want it to be able to do is return a character array.

Here is the code:

#import <stdio.h>

void cls() {
    system(cls);
}

char input() {
    char X[0];
    int Y = 0;
    char Z;
    while (1 == 1) {
        Y = Y + 1; //increment step
        Z = getch(); //get character input
        if (Z == ';') {
            break;
        } //break if stop character is pushed
        char Q[Y];
        int a;
        for (a = 0; a < Y; a = a + 1) {
            Q[a] = X[a];
        } //pass the items of X to the larger Q array
        Q[Y] = Z; //add new value to Q at end of array
        char X[Y];
        int b;
        for (b = 0; b < Y + 1; b = b + 1) {
            X[a] = Q[a];
        } //pass the items of Q back to a larger X
        printf(X);
    }
    return X;
}

int main() {
    while (1 == 1) {
        //stuff will go here later
    }
    return 0;
}
Draden Merenox
  • 305
  • 2
  • 10
  • 1
  • 1
    You have *multiple* arrays named `X`, each different from the other. And then you return the top-level `X` which is *empty* (which is not allowed really). Furthermore, returning a pointer to an element of a local array will return an *invalid pointer* as the array goes out of scope and cease to exits immediately. – Some programmer dude Nov 26 '17 at 04:09
  • 2
    You also have many other errors, like what is `#import`? And the [`getch` (or rather `_getch`) function](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/getch-getwch) returning an *`int`* (which is kind of important if you want to check for errors from it, which you should). And the `cls` function will probably crash if you try to call it. Perhaps it's time for you to take a few steps back, [get a good beginners book or two](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list), and just about start over. – Some programmer dude Nov 26 '17 at 04:12
  • How should I fix the code then? Delete X after passing the values to Q, then redeclare X, and same with Q? – Draden Merenox Nov 26 '17 at 04:12
  • 1
    "Delete X after passing the values to Q, then redeclare X" -- That doesn't sound right at all. You should get [a good C book](https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) to learn from. C is not a good language to learn by guesswork. And this is probably the fifth question _today_ I have seen on SO about returning an array from a function. Surely you can find some answers by searching. – ad absurdum Nov 26 '17 at 04:25
  • `-Wshadow` is a handy option to add to your compile string. – David C. Rankin Nov 26 '17 at 05:10

2 Answers2

0

Arrays are not first-class types in C, so you can't return them from functions, nor pass them as arguments to a function. You need to pass/return pointers instead. But as the pointers must point to something (to be useful), you need to keep track of who "owns" what the pointers point at -- who is responsible for allocation and freeing the memeory involved.

So that said, there are several ways you can manage passing and returning (pointers to) arrays.

  • You can have the caller allocate an array (either on the stack or the heap), and pass a pointer (and size) to that array to a function that will fill it in. The called function in this case doesn't have to worry about allocating and freeing the array, but does need to worry about not overflowing it.

  • You can have the callee allocate an array on the heap and return a pointer to the caller. You can't do this with callee stack allocated memory, as the all callee stack allocated memory is freed when the callee returns, so if you try to return a pointer to it, the pointer will be dangling. A heap allocated array avoids that problem, but means that the caller will need to free it, and has the additional difficulty that the caller doesn't know how large the allocated array is, unless you return a struct containing that info along with th pointer, or somehow communicate that info back to the caller.

Chris Dodd
  • 119,907
  • 13
  • 134
  • 226
0

You should do following:

errors saying "return makes integer without a cast"

Change return type of function input from char to char *.

errors saying "function returns address of local variable"

The memory returned my variable X should be allocated dynamically i.e. using calloc or malloc function. Because in provided code the memory hold my variable X is local and present in stack. So when function call returns from

cse
  • 4,066
  • 2
  • 20
  • 37