How to convert char pointer into char in C Open VMS

user692121 picture user692121 · Oct 17, 2011 · Viewed 23.5k times · Source

This is to convert from char pointer into char.

I followed the codes from another topic but it seems like it's not working to me. I am using Open VMS Ansi C compiler for this. I don't know what's the difference with another Platform.

main(){

char * field = "value1";
char c[100] = (char )field;

printf("c value is %s",&c);

}

the output of this is

c value is

which is unexpected for me I am expecting

c value is value1

hope you can help me.

Answer

Matthew Flaschen picture Matthew Flaschen · Oct 17, 2011
strcpy(c, field);

You must be sure c has room for all the characters in field, including the NUL-terminator. It does in this case, but in general, you will need an if check.

EDIT: In C, you can not return an array from a function. If you need to allocate storage, but don't know the length, use malloc. E.g.:

size_t size = strlen(field) + 1; //  If you don't know the source size.
char *c = malloc(size);

Then, use the same strcpy call as before.