user4988137
user4988137

Reputation:

How to store result of type char in pointer?

I want to store result of type char in pointer which I'm passing as argument of function. Like this:

#include<stdio.h>
void try(char *);
int main()
{
    char *result;
    try(result);
    printf("%s",result);
    return 0;
}
void try(char *result)
{
    result="try this";
}

But I'm getting result : (null)

Could someone tell me what's wrong here?

Upvotes: 0

Views: 714

Answers (3)

Giorgi Moniava
Giorgi Moniava

Reputation: 28654

Also another way (no dynamic memory):

#include<stdio.h>
void try(char *);
int main()
{
    char result[100] = {0};
    try(result);
    printf("%s",result);
    return 0;
}
void try(char *result)
{
    strcpy(result,"try this");
}

Note: When you say you got null, that doesn't mean anything - actually you had undefined behaviour there - because result was not initialized. I guess you invoked UB even before trying to print result, namely when you passed it to try. Because copy would be made in that method of the pointer, which would try to read value of original pointer - reading uninitialized variables is undefined in C. Hence always initialize your variables in C.

Upvotes: 0

Karup
Karup

Reputation: 2079

You are creating another variable result inside try function. Try printing result inside try function. It will work then.

If you really want to print inside main then try this -

#include<stdio.h>

void try(char **);

int main()
{
    char *result;
    try(&result);
    printf("%s",result);
    return 0;
}
void try(char** result)
{
    *result = "try this";
    //printf("%s\n",result);
}

Or if you don't want to get into double pointers, then this will work:

#include<stdio.h>

char* try(char *);

int main()
{
    char *result;
    result = try(result);
    printf("%s",result);
    return 0;
}
char* try(char* result)
{
    result = "try this";
    return result;
}

Upvotes: 1

Sami Kuhmonen
Sami Kuhmonen

Reputation: 31153

Your syntax only sends the pointer to the function. This allows changing the data the pointer points to, but not the pointer itself.

You would need to have

void try(char **result)

and call it

try(&result);

to change the actual pointer.

Another way is to copy data into the memory pointed by the pointer, but then you need to know there is enough memory available. Depends on the actual use case how to do it properly. You might use

strcpy(result, "what you want");

but then you really have to know that the memory pointed by result can handle 14 chars (remember the NULL in the end). In your current code you don't allocate memory at all for result, so this will invoke undefined behaviour.

The reason you're seeing NULL is because your compiler decided to initialize non-assigned pointers to NULL. Another compiler might initialize them to random values.

Also about terminology, you're not storing type char into a pointer. You may have a pointer pointing to a char, or in this case to a C type string, which is an array of chars.

Upvotes: 2

Related Questions