0
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stdbool.h>
char** parser(char *message)
{
    char a[9][256];
    char* tmp =message;
    bool inQuote=0;
    int counter=0;
    int counter2=0;
    while(*tmp!='\0')
    {
        switch(*tmp)
        {
            case ',': if(!inQuote)
                   {    
                    a[counter][counter2]='\0';
                    printf("A[%d]: %s\n",counter,a[counter]);
                    counter++;
                    counter2=0;
                    }

                  break;
            case '"':
                inQuote=!inQuote;
                break;
            default:
                a[counter][counter2]=*tmp;
                counter2++;
                break;
        }
    tmp++;

    }
        a[counter][counter2]='\0';
        printf("A[%d]: %s\n",counter,a[counter]);
        return(a);

}
int main()
{
    char **a = parser("N,8545,01/02/2011 09:15:01.815,\"RASTA OPTSTK 24FEB2011 1,150.00 CE\",S,8.80,250,0.00,0");
    return 0;
}

The error given is at Line 38 : return from incompatible pointer type and function returns address of local variable

EDIT Can somebody modify the code accordingly so that I can access (via pointer) the contents of 'a' from main();

Soham
  • 863
  • 2
  • 19
  • 35

4 Answers4

2

The errors speak for themselves

Line 38 : return from incompatible pointer type

a has been defined as char a[9][256]. So, in the statement return(a);, the type of value being returned is char (*)[256] ( pointer to an array of 256 chars ) and not char ** ( as per the prototype of parser() )

function returns address of local variable

Well, a is a variable local to the function. You should not be returning address of local variables ( unless memory for it has been dynamically allocated or it is a static variable )

Anand
  • 473
  • 3
  • 14
  • So the return type of the function should be what, char(*)[256] ? It had been quite some time for me, delving deep into C, but I remember something as a 2D array being manipulated using pointer to pointers. – Soham Feb 16 '11 at 13:14
  • 1
    @Soham : You can either pass the array you need to modify. Refer to this : http://stackoverflow.com/questions/2959526/pass-2d-array-to-function-in-c OR You can change the prototype to char (*parser(char *message))[], change the definition of 'a' in parser() to make it static and change the definition of 'a' in main() to char (*a)[256]; The first approach is better – Anand Feb 16 '11 at 15:45
1

It is not advisable to return the address of a local variable outside the function as they are stored in the the stack, and should not be accessed by outside the function. The incompatible type warning can be removed by proper typecasting - but again, you shouldn't return the address of local variable.

vtha
  • 577
  • 4
  • 7
1

In your code you are returning pointer but in the function definition you have declared it to be returning pointer to pointer that is the reason why you are encountering error.Also since the local variables are created in stack and once the control returns to calling function stack is cleared thus you are referencing to a location which does not contain data.Thus it would be better to declare your 2D array of character to be static.

Algorithmist
  • 6,657
  • 7
  • 35
  • 49
  • But then how do I return this array 'a'to main function. – Soham Feb 16 '11 at 13:28
  • @soham as i have stated you make this array as static so that its lifetime would be uptil the program is closed. – Algorithmist Feb 16 '11 at 13:52
  • Is there a better way to achieve this, by passing pointer to this array to the main function?I am interested in that bit. Declaring it as static, and returning the address of `a` still flags the warning `return from incompatible pointer type` – Soham Feb 16 '11 at 14:11
  • @soham what you can do is declare this array in main and pass this as a parameter to your function.As you must be knowing arrays are passed by pointers so any changes you would made in the array inside the function would be reflected in main. – Algorithmist Feb 16 '11 at 14:35
1

char** parser(char message) { char a[9][256]; char tmp =message; bool inQuote=0;

MUST be

char** parser(char message) { static char a[9][256]; char tmp =message; bool inQuote=0;

Pete Wilson
  • 8,610
  • 6
  • 39
  • 51