简体   繁体   中英

Dereference C string pointer into variable

I have the following simple program which creates a pointer to the first character of a string:

char str[] = "Hello world";                                                   
char *p = &str[0];

How can I then get this string back into a variable using only the pointer?

Dereferencing the pointer just gives the first character of the string - as somewhat expected - so I'm assuming that there is no 'simple' way to achieve this and it will instead require writing extra code.

The current way I would approach this would be as follows:

  1. Iterate from the pointer until a null terminator is reached to find the length of the string
  2. Create a new char array with this length
  3. Iterate through again inserting characters into this array

Is there a library function to achieve this, or if not, a simpler way that doesn't involve iterating twice?

Yes you have to "do it by hand". Because there are no objects in C - you need to take care of all that happens in the code.

You can use malloc , strlen and memcpy :

char str[] = "Hello world";                                                   
char *p = malloc(strlen(str) + 1);
if (!p) { abort(); }
memcpy(p, str, strlen(str) + 1);

You can use strcpy and forget about one strlen :

char *p = malloc(strlen(str) + 1);
if (!p) { abort(); }
strcpy(p, str);

Or you can use strdup from POSIX or a C extension:

char *p = strdup(str);
if (!p) { abort(); }

...

Is there a library function to achieve this, or if not, a simpler way that doesn't involve iterating twice?

As said in comment, strdup() will do exactly what you want. But here there is another problem (by your point of view): strcpy() will iterate the string twice, because there is no other way to duplicate a string.

By definition, strings in C are a sequence of characters somewhere in memory, with the last one character being a NUL (with single L), the value 0 (in a char). References to strings are pointers to the first character in the sequence depicted above. Note that two different strings can point to the same memory (they are not so different then...), or a string can point into the middle of another. These two cases are somewhat particular but not uncommon. The memory for strings must be managed by the programmer, who is the only one to know where allocate and deallocate space for strings; functions like strcpy() do nothing special in this regard, they are (presumably) well written and optimized, so maybe to copy a string the behavior is not plain as I depicted it before, but the idea is the same.

try this code:

#include "stdio.h"
int main(){
char str[] = "Hello world";                                                   
int count = 12; 
char (*p)[12] = &str;
printf("%c\n",(*p)[0]);
printf("%c\n",(*p)[1]);
printf("%c\n",(*p)[2]);
printf("%c\n",(*p)[3]);
printf("%s\n",(*p)); 
}

Here's how I would make a copy of a string using only the standard library functions:

#include <stdio.h>  // printf
#include <stdlib.h> // malloc
#include <string.h> // strcpy

int main(void)
{
    char str[] = "Hello world"; // your original string
    char *p = (char *)malloc(strlen(str) + 1); // allocate enough space to hold the copy in p
    if (!p) { // malloc returns a NULL pointer when it fails
        puts("malloc failed.");
        exit(-1);
    }
    strcpy(p, str); // now we can safely use strcpy to put a duplicate of str into p
    printf("%s\n", p); // print out this duplicate to verify
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM