Rachel Bacon
Rachel Bacon

Reputation: 31

Assign a char pointer the value of another char pointer

I have two char pointers:

char *temp;
char *saveAlias;

I want to assign saveAlias with whatever is stored in temp; saveAlias is currently empty while temp has a string of an unknown size saved from user input. Note that I don't want saveAlias to point to where temp points; I want the content of temp and assign (get pointed) it to saveAlias.

I have attempted using strcat and strcpy but to no avail.

Upvotes: 2

Views: 2770

Answers (3)

Kennet Emerson
Kennet Emerson

Reputation: 56

You can basically point the saveAlias to temp; Thus you would have:

saveAlias = temp;

As noted by Chris. This will make one pointer point to another. Correcting my answer. I suggest you define de size of saveAlias with malloc, and then use the memcpy function. You will have:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
  
int main(){    
   char *temp = "your_char";

   //with malloc you make sure saveAlias will have the same size as temp
   //and then we add one for the NULL terminator
   char *saveAlias = (char*) malloc(strlen(temp) + 1);
            
   //then just                                                                                                                                            
   strcpy(saveAlias, temp);
 
   printf("%s\n", temp);
   printf("%s", saveAlias);
   return 0;            
}

Thank you chqrlie for the explanation also. I was mistaken about the memcpy.

Upvotes: 0

chqrlie
chqrlie

Reputation: 144695

If you want to allocate memory for a copy of the string currently pointed to by temp, use strdup():

#include <stdio.h>
#include <string.h>

int main() {
    char buf[128];
    char *temp;
    char *saveAlias = NULL;

    if ((temp = fgets(buf, sizeof buf, stdin)) != NULL) {
        saveAlias = strdup(temp);
        if (saveAlias == NULL) {
            fprintf(stderr, "allocation failed\n");
        } else {
            printf("saveAlias: %s\n", saveAlias);
        }
    }
    free(saveAlias);
    return 0;
}

Upvotes: 1

Adrian Mole
Adrian Mole

Reputation: 51825

Assuming that your temp variable points to a character string that is suitably nul-terminated (as strings in C should be), then you can just use the strdup() function to make a copy of it and store a pointer to that in saveAlias. This function will duplicate the given string into newly-allocated memory; that memory should be released, using the free function, when no longer needed:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main()
{
    char* temp = "source string";
    char* saveAlias = strdup(temp);
    printf("Temp is <%s> (at %p).\n", temp, (void*)temp);
    printf("Alias is <%s> (at %p).\n", saveAlias, (void*)saveAlias);
    free(saveAlias);
    return 0;
}

The strdup function effectively combines malloc and strcpy into a single function call, with the call shown above being the equivalent of:

    char* saveAlias = malloc(strlen(temp) + 1);
    strcpy(saveAlias, temp);

Upvotes: 1

Related Questions