1

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.

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
  • hello thanks for your answers guys... it did work. But can I create my own function like for this scenario example conv_ptr_str function that converts pointer char to char; say char *field = "value1"; char c[100] = (char )field; char c = conv_ptr_str(field); So that I could re use the resources or this is the only way to convert things? –  Oct 17 '11 at 04:55

3 Answers3

4
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.

Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539
1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(){

    char * field = "value";

    char c[100]="";
    strncpy(c,field,100);

    printf("c value is %s",c);
    return 0;
}
Al Foиce ѫ
  • 4,195
  • 12
  • 39
  • 49
  • While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. – Al Foиce ѫ Jun 18 '21 at 08:46
0

In C, the char type holds a single character, not a string. char c[100]; doesn't allocate a char of length 100, it allocates an array of 100 consecutive chars, each one byte long, and that array can hold a string.

So what you seem to want to do is to fill an array of chars with the same char values that are at the location pointed at by a char *. To do that, you can use strncpy() or any of several other functions:

strncpy(c,field,100); /* copy up to 100 chars from field to c */
c[99] = '\0';         /* ..and make sure the last char in c is '\0' */

..or use strcpy() since you know the string will fit in c (better in this case):

strcpy(c,field);

..or:

snprintf(c,100,"%s",field);
Dmitri
  • 9,175
  • 2
  • 27
  • 34
  • 1
    `strncpy()` is *not* just a safer version of `strcpy()` (as `snprintf()` is a safer version of `sprintf()`). `strncpy()` can leave the target buffer unterminated (i.e., not containing a string), or can needlessly pad it with multiple `'\0'` characters when only one is necessary. You're better off using `strcpy()` after confirming that the target buffer is big enough. – Keith Thompson Oct 17 '11 at 04:35
  • @KeithThompson The string is explicitly null-terminated in the example above. You do have a point about potentially padding extra null characters... though in cases where the source string's length isn't known calling `strlen()` before `strcpy()` isn't necessarily faster. – Dmitri Oct 17 '11 at 05:02