Before starting, I should say I have seen several posts around this topic (like this one, but I am still missing something. I am quite new to C, so please bear with me. I am attempting to construct a function that copies a string from one pointer location to another.
#include <stdio.h>
#include <malloc.h>
#include <string.h>
void astrncpy(char **base, char *copyme){
printf("Copying '%s' into a location currently featuring the following string: '%s'\n", copyme,*base);
printf("The location of our string to be copied (%s) is %d.\n", copyme, ©me);
printf("The location of our string to be replaced (%s) is %d.\n", *base, base);
//Declare string length variable
int new_len=strlen(copyme);
printf("Calculating new length for replaced memory allocation (%d).\n",new_len);
//Reallocate pointer array
*base=realloc(*base,sizeof(char)*new_len+1);
printf("Reallocating memory allocation.\n");
//Copy copyme content to base string location
strncpy(*base,copyme,new_len);
printf("The string at location %d is now %s\n", base, *base);
}
void main(){
//Declare iterator
int i;
//Generate strings
char first_lit[]="Fortran?";
char second[]="Now that's a name I've not heard in a long time.";
//Convert first string to array (so we can get at the pointer to the strings pointer)
char **first; //Declare pointer to pointer array that represents the first string
first=malloc(strlen(first_lit)*sizeof(char)); //Allocate space for the pointer array
*first=first_lit; //Assign values to the pointer locations
//Copy copyme into base
astrncpy(first,second);
}
When I attempt to reallocate within astrncpy()
, the core dumps. From what I understand, this occurs if the pointer array is either not NULL, or is not the product of malloc()
. I feel like I am not failing that test. Any guidance on what is happening would be appreciated.
Bonus points for insight on my construction of the input *base
. I spent a good deal of time experimenting to determine what an acceptable input would be for the **base
argument (note that both arguments to astrncpy()
are given in the text I am working through). I am not clear, however, on why I can't get there with first_lit
instead of having to construct first
. The string is still an array, is it not? Again, help is appreciated.
UPDATE: I put this down for a while to do work I actually get paid to do, but coming back to it, I couldn't shake the idea that the declaration modifications in the response below weren't necessary. (This is because they had not yet been covered in the text.) In any event, I will still give a check to the solution because it works and was helpful on multiple fronts. It should be noted, however, that the following also works:
/*Program demonstrates string manipulation operations*/
#include <stdio.h>
#include <malloc.h>
#include <string.h>
void astrncpy(char **base, char *copyme){
printf("\nWe are inside the function, astrncpy.\n");
printf("\nCopying '%s' (*copyme) into a location currently featuring the following string: '%s' (**base)\n", copyme,*base);
printf("The location of our string to be copied (%s) is %p.\n", copyme, ©me);
printf("The location of our string to be replaced (%s) is %p.\n", *base, base);
//Declare string length variable
size_t new_len=strlen(copyme);
printf("Calculating new length for replaced memory allocation (%d).\n",new_len);
//Reallocate pointer array
printf("Reallocating memory block associated with base string to be replaced.\n");
*base=realloc(*base,sizeof(char)*(new_len+1));
//Copy copyme content to base string location
strncpy(*base,copyme,new_len);
printf("The string at location %p is now %s\n", base, *base);
}
void main(){
//Declare iterator
int i;
//Generate strings
char first_lit[]="Fortran?";
char second[]="Now that's a name I've not heard in a long time.";
//int testint=5;
//Capture elements of first_lit in pointer array (so we can get at the pointer to the strings pointer)
char *first; //Declare pointer to pointer array that represents the first string
first=malloc((strlen(first_lit)+1)*sizeof(char)); //Allocate space for the pointer array
strncpy(first,first_lit,strlen(first_lit)); //Assign values to the pointer locations
//Copy copyme into base
printf("Initiating copy operation...\n");
astrncpy(&first,second);
}