malloc(): memory corruption on string concatenation - c

Hello i am doing a project where i have to implement a hashTable that stores words based on a hash function. On a stress test i get malloc(): memory corruption
The initial declaration of the hashTable
hashTable = (char**)malloc(hashSize[0] * sizeof(char*));
This is the function i wrote to add word to hashTable of hashSize:
void addWord(char** hashTable, unsigned int hashSize, const char* word) {
int bucketIndex = hash(word, hashSize);
//printf("Word to add = %s, bucket = %d, hashTable size = %d\n", word, bucketIndex, hashSize);
if(hashTable[bucketIndex] == NULL) {
hashTable[bucketIndex] = (char*)malloc(strlen(word) * sizeof(char));
strcpy(hashTable[bucketIndex], word);
return;
}
/* checks for duplicats */
int exists = 0;
char* heyStack = (char*)malloc(strlen(hashTable[bucketIndex]));
memcpy(heyStack, hashTable[bucketIndex], strlen(hashTable[bucketIndex]));
char* token = strtok(heyStack, " ");
while(token) {
if(strcmp(token, word) == 0) {
exists = 1;
break;
}
token = strtok(NULL, " ");
}
/* end check for duplicates */
if(exists == 0) {
size_t bucketSize = strlen(hashTable[bucketIndex]);
hashTable[bucketIndex] = (char*)realloc(hashTable[bucketIndex], bucketSize + strlen(word) + 2);
memcpy(hashTable[bucketIndex] + bucketSize, " ", 1);
memcpy(hashTable[bucketIndex] + bucketSize + 1, word, strlen(word) + 1);
}
}
I have a stress test that adds 20k words to the table and it always breaks on the same word (no 10k something)
Any ideas on what i am doing wrong ?
Tyvm

You have to terminate the "strings" before passing it to functions that deal with strings, such as strlen() and strtok().
Allocate size of the string and one more byte for terminating null-character.
Terminate the "strings" by adding null character.
Notes:
They say you shouldn't cast the result of malloc() in C.
sizeof(char) will always be 1, so you don't need to multiply with it.
Corrected code:
void addWord(char** hashTable, unsigned int hashSize, const char* word) {
int bucketIndex = hash(word, hashSize);
//printf("Word to add = %s, bucket = %d, hashTable size = %d\n", word, bucketIndex, hashSize);
if(hashTable[bucketIndex] == NULL) {
size_t wordSize = strlen(word);
hashTable[bucketIndex] = malloc(wordSize + 1); /* size +1 */
memcpy(hashTable[bucketIndex], word, wordSize + 1); /* why did you use strcpy() only in here? */
return;
}
/* checks for duplicats */
int exists = 0;
size_t dataSize = strlen(hashTable[bucketIndex]);
char* heyStack = malloc(dataSize + 1); /* size +1 */
memcpy(heyStack, hashTable[bucketIndex], dataSize + 1); /* size +1 */
char* token = strtok(heyStack, " ");
while(token) {
if(strcmp(token, word) == 0) {
exists = 1;
break;
}
token = strtok(NULL, " ");
}
/* end check for duplicates */
if(exists == 0) {
size_t bucketSize = strlen(hashTable[bucketIndex]);
size_t wordSize = strlen(word);
hashTable[bucketIndex] = realloc(hashTable[bucketIndex], bucketSize + wordSize + 2);
memcpy(hashTable[bucketIndex] + bucketSize, " ", 1);
memcpy(hashTable[bucketIndex] + bucketSize + 1, word, wordSize + 1);
}
free(heyStack); /* do free what you allocated */
}
This code will be better if you add some code to check if malloc() and realloc() are successful.

Related

Copying specific number of characters from a string to another

I have a variable length string that I am trying to divide from plus signs and study on:
char string[] = "var1+vari2+varia3";
for (int i = 0; i != sizeof(string); i++) {
memcpy(buf, string[0], 4);
buf[9] = '\0';
}
since variables are different in size I am trying to write something that is going to take string into loop and extract (divide) variables. Any suggestions ? I am expecting result such as:
var1
vari2
varia3
You can use strtok() to break the string by delimiter
char string[]="var1+vari2+varia3";
const char delim[] = "+";
char *token;
/* get the first token */
token = strtok(string, delim);
/* walk through other tokens */
while( token != NULL ) {
printf( " %s\n", token );
token = strtok(NULL, delim);
}
More info about the strtok() here: https://man7.org/linux/man-pages/man3/strtok.3.html
It seems to me that you don't just want to want to print the individual strings but want to save the individual strings in some buffer.
Since you can't know the number of strings nor the length of the individual string, you should allocate memory dynamic, i.e. use functions like realloc, calloc and malloc.
It can be implemented in several ways. Below is one example. To keep the example simple, it's not performance optimized in anyway.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
char** split_string(const char* string, const char* token, int* num)
{
assert(string != NULL);
assert(token != NULL);
assert(num != NULL);
assert(strlen(token) != 0);
char** data = NULL;
int num_strings = 0;
while(*string)
{
// Allocate memory for one more string pointer
char** ptemp = realloc(data, (num_strings + 1) * sizeof *data);
if (ptemp == NULL) exit(1);
data = ptemp;
// Look for token
char* tmp = strstr(string, token);
if (tmp == NULL)
{
// Last string
// Allocate memory for one more string and copy it
int len = strlen(string);
data[num_strings] = calloc(len + 1, 1);
if (data[num_strings] == NULL) exit(1);
memcpy(data[num_strings], string, len);
++num_strings;
break;
}
// Allocate memory for one more string and copy it
int len = tmp - string;
data[num_strings] = calloc(len + 1, 1);
if (data[num_strings] == NULL) exit(1);
memcpy(data[num_strings], string, len);
// Prepare to search for next string
++num_strings;
string = tmp + strlen(token);
}
*num = num_strings;
return data;
}
int main()
{
char string[]="var1+vari2+varia3";
// Split the string into dynamic allocated memory
int num_strings;
char** data = split_string(string, "+", &num_strings);
// Now data can be used as an array-of-strings
// Example: Print the strings
printf("Found %d strings:\n", num_strings);
for(int i = 0; i < num_strings; ++i) printf("%s\n", data[i]);
// Free the memory
for(int i = 0; i < num_strings; ++i) free(data[i]);
free(data);
}
Output
Found 3 strings:
var1
vari2
varia3
You can use a simple loop scanning the string for + signs:
char string[] = "var1+vari2+varia3";
char buf[sizeof(string)];
int start = 0;
for (int i = 0;;) {
if (string[i] == '+' || string[i] == '\0') {
memcpy(buf, string + start, i - start);
buf[i - start] = '\0';
// buf contains the substring, use it as a C string
printf("%s\n", buf);
if (string[i] == '\0')
break;
start = ++i;
} else {
i++;
}
}
Your code does not have any sense.
I wrote such a function for you. Analyse it as sometimes is good to have some code as a base
char *substr(const char *str, char *buff, const size_t start, const size_t len)
{
size_t srcLen;
char *result = buff;
if(str && buff)
{
if(*str)
{
srcLen = strlen(str);
if(srcLen < start + len)
{
if(start < srcLen) strcpy(buff, str + start);
else buff[0] = 0;
}
else
{
memcpy(buff, str + start, len);
buff[len] = 0;
}
}
else
{
buff[0] = 0;
}
}
return result;
}
https://godbolt.org/z/GjMEqx

Free, invalid pointer

I have a program, that splits strings based on the delimiter. I have also, 2 other functions, one that prints the returned array and another that frees the array.
My program prints the array and returns an error when the free array method is called. Below is the full code.
#include "stringsplit.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
/* Split string by another string, return split parts + NULL in array.
*
* Parameters:
* str: the string to split
* split: the string to split str with
*
* Returns:
* A dynamically reserved array of dynamically reserved string parts.
*
* For example called with "Test string split" and " ",
* returns ["Test", "string", "split", NULL].
* Or called with "Another - test" and " - ",
* returns ["Another", "test", NULL].
*/
unsigned long int getNofTokens(const char *string) {
char *stringCopy;
unsigned long int stringLength;
unsigned long int count = 0;
stringLength = (unsigned)strlen(string);
stringCopy = malloc((stringLength + 1) * sizeof(char));
strcpy(stringCopy, string);
if (strtok(stringCopy, " \t") != NULL) {
count++;
while (strtok(NULL, " \t") != NULL)
count++;
}
free(stringCopy);
return count;
}
char **split_string(const char *str, const char *split) {
unsigned long int count = getNofTokens(str);
char **result;
result = malloc(sizeof(char *) * count + 1);
char *tmp = malloc(sizeof(char) * strlen(str));
strcpy(tmp, str);
char *token = strtok(tmp, split);
int idx = 0;
while (token != NULL) {
result[idx++] = token;
token = strtok(NULL, split);
}
return result;
}
void print_split_string(char **split_string) {
for (int i = 0; split_string[i] != NULL; i++) {
printf("%s\n", split_string[i]);
}
}
void free_split_string(char **split_string) {
for (int i = 0; split_string[i] != NULL; i++) {
char *currentPointer = split_string[i];
free(currentPointer);
}
free(split_string);
}
Also, do I need to explicitly add \0 at the end of the array or does strtok add it automatically?
There are some problems in your code:
[Major] the function getNofTokens() does not take the separator string as an argument, it counts the number of words separated by blanks, potentially returning an inconsistent count to its caller.
[Major] the size allocated in result = malloc(sizeof(char *) * count + 1); is incorrect: it should be:
result = malloc(sizeof(char *) * (count + 1));
Storing the trailing NULL pointer will write beyond the end of the allocated space.
[Major] storing the said NULL terminator at the end of the array is indeed necessary, as the block of memory returned by malloc() is uninitialized.
[Major] the copy of the string allocated and parsed by split_string cannot be safely freed because the pointer tmp is not saved anywhere. The pointer to the first token will be different from tmp in 2 cases: if the string contains only delimiters (no token found) or if the string starts with a delimiter (the initial delimiters will be skipped). In order to simplify the code and make it reliable, each token could be duplicated and tmp should be freed. In fact your free_split_string() function relies on this behavior. With the current implementation, the behavior is undefined.
[Minor] you use unsigned long and int inconsistently for strings lengths and array index variables. For consistency, you should use size_t for both.
[Remark] you should allocate string copies with strdup(). If this POSIX standard function is not available on your system, write a simple implementation.
[Major] you never test for memory allocation failure. This is OK for testing purposes and throw away code, but such potential failures should always be accounted for in production code.
[Remark] strtok() is a tricky function to use: it modifies the source string and keeps a hidden static state that makes it non-reentrant. You should avoid using this function although in this particular case it performs correctly, but if the caller of split_string or getNofTokens relied on this hidden state being preserved, it would get unexpected behavior.
Here is a modified version:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "stringsplit.h"
/* Split string by another string, return split parts + NULL in array.
*
* Parameters:
* str: the string to split
* split: the string to split str with
*
* Returns:
* A dynamically reserved array of dynamically reserved string parts.
*
* For example called with "Test string split" and " ",
* returns ["Test", "string", "split", NULL].
* Or called with "Another - test" and " - ",
* returns ["Another", "test", NULL].
*/
size_t getNofTokens(const char *string, const char *split) {
char *tmp = strdup(string);
size_t count = 0;
if (strtok(tmp, split) != NULL) {
count++;
while (strtok(NULL, split) != NULL)
count++;
}
free(tmp);
return count;
}
char **split_string(const char *str, const char *split) {
size_t count = getNofTokens(str, split);
char **result = malloc(sizeof(*result) * (count + 1));
char *tmp = strdup(str);
char *token = strtok(tmp, split);
size_t idx = 0;
while (token != NULL && idx < count) {
result[idx++] = strdup(token);
token = strtok(NULL, split);
}
result[idx] = NULL;
free(tmp);
return result;
}
void print_split_string(char **split_string) {
for (size_t i = 0; split_string[i] != NULL; i++) {
printf("%s\n", split_string[i]);
}
}
void free_split_string(char **split_string) {
for (size_t i = 0; split_string[i] != NULL; i++) {
free(split_string[i]);
}
free(split_string);
}
Here is an alternative without strtok() and without intermediary allocations:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "stringsplit.h"
size_t getNofTokens(const char *str, const char *split) {
size_t count = 0;
size_t pos = 0, len;
for (pos = 0;; pos += len) {
pos += strspn(str + pos, split); // skip delimiters
len = strcspn(str + pos, split); // parse token
if (len == '\0')
break;
count++;
}
return count;
}
char **split_string(const char *str, const char *split) {
size_t count = getNofTokens(str, split);
char **result = malloc(sizeof(*result) * (count + 1));
size_t pos, len, idx;
for (pos = 0, idx = 0; idx < count; pos += len, idx++) {
pos += strspn(str + pos, split); // skip delimiters
len = strcspn(str + pos, split); // parse token
if (len == '\0')
break;
result[idx] = strndup(str + pos, len);
}
result[idx] = NULL;
return result;
}
void print_split_string(char **split_string) {
for (size_t i = 0; split_string[i] != NULL; i++) {
printf("%s\n", split_string[i]);
}
}
void free_split_string(char **split_string) {
for (size_t i = 0; split_string[i] != NULL; i++) {
free(split_string[i]);
}
free(split_string);
}
EDIT After re-reading the specification in your comment, there seems to be some potential confusion as to the semantics of the split argument:
if split is a set of delimiters, the above code does the job. And the examples will be split as expected.
if split is an actual string to match explicitly, the above code only works by coincidence on the examples given in the comment.
To implement the latter semantics, you should use strstr() to search for the split substring in both getNofTokens and split_string.
Here is an example:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "stringsplit.h"
/* Split string by another string, return split parts + NULL in array.
*
* Parameters:
* str: the string to split
* split: the string to split str with
*
* Returns:
* A dynamically reserved array of dynamically reserved string parts.
*
* For example called with "Test string split" and " ",
* returns ["Test", "string", "split", NULL].
* Or called with "Another - test" and " - ",
* returns ["Another", "test", NULL].
*/
size_t getNofTokens(const char *str, const char *split) {
const char *p;
size_t count = 1;
size_t len = strlen(split);
if (len == 0)
return strlen(str);
for (p = str; (p = strstr(p, split)) != NULL; p += len)
count++;
return count;
}
char **split_string(const char *str, const char *split) {
size_t count = getNofTokens(str, split);
char **result = malloc(sizeof(*result) * (count + 1));
size_t len = strlen(split);
size_t idx;
const char *p = str;
for (idx = 0; idx < count; idx++) {
const char *q = strstr(p, split);
if (q == NULL) {
q = p + strlen(p);
} else
if (q == p && *q != '\0') {
q++;
}
result[idx] = strndup(p, q - p);
p = q + len;
}
result[idx] = NULL;
return result;
}
void print_split_string(char **split_string) {
for (size_t i = 0; split_string[i] != NULL; i++) {
printf("%s\n", split_string[i]);
}
}
void free_split_string(char **split_string) {
for (size_t i = 0; split_string[i] != NULL; i++) {
free(split_string[i]);
}
free(split_string);
}
When debugging, take note of values that you got from malloc, strdup, etc. Let's call these values "the active set". It's just a name, so that we can refer to them. You get a pointer from those functions, you mentally add it to the active set. When you call free, you can only pass values from the active set, and after free returns, you mentally remove them from the set. Any other use of free is invalid and a bug.
You can easily find this out by putting breakpoints after all memory allocations, so that you can write down the pointer values, and then breakpoints on all frees, so that you can see if one of those pointer values got passed to free - since, again, to do otherwise is to misuse free.
This can be done also using "printf" debugging. Like this:
char *buf = malloc(...); // or strdup, or ...
fprintf(stderr, "+++ Alloc %8p\n", buf);
And then whenever you have free, do it again:
fprintf(stderr, "--- Free %8p\n", ptr);
free(ptr);
In the output of the program, you must be able to match every +++ with ---. If you see any --- with a value that wasn't earlier listed with a +++, there's your problem: that's the buggy invocation of free :)
I suggest using fprintf(stderr, ... instead of printf(..., since the former is typically unbuffered, so if your program crashes, you won't miss any output. printf is buffered on some architectures (and not buffered on others - so much for consistency).

Reimplementing split function in C

I have been trying to write a function that takes in strings as a line and returns a pointer to an array of words. The function written below does something similar
How can I rewrite the following code1 but it should be better than code2 by being able to change the delimiter. However, code1 works but during memory allocation the same memory is duplicated for the words array. Thereby causing word duplication.
Code 1:
char *split(const char *string) {
char *words[MAX_LENGTH / 2];
char *word = (char *)calloc(MAX_WORD, sizeof(char));
memset(word, ' ', sizeof(char));
static int index = 0;
int line_index = 0;
int word_index = 0;
while (string[line_index] != '\n') {
const char c = string[line_index];
if (c == ' ') {
word[word_index+ 1] = '\0';
memcpy(words + index, &word, sizeof(word));
index += 1;
if (word != NULL) {
free(word);
char *word = (char *)calloc(MAX_WORD, sizeof(char));
memset(word, ' ', sizeof(char));
}
++line_index;
word_index = 0;
continue;
}
if (c == '\t')
continue;
if (c == '.')
continue;
if (c == ',')
continue;
word[word_index] = c;
++word_index;
++line_index;
}
index = 0;
if (word != NULL) {
free(word);
}
return *words;
}
Code 2:
char **split(char *string) {
static char *words[MAX_LENGTH / 2];
static int index = 0;
// resetting words
for (int i = 0; i < sizeof(words) / sizeof(words[0]); i++) {
words[i] = NULL;
}
const char *delimiter = " ";
char *ptr = strtok(string, delimiter);
while (ptr != NULL) {
words[index] = ptr;
ptr = strtok(NULL, delimiter);
++index;
}
index = 0;
return words;
}
However I noticed that the memory of word+index is been reassigned to the same location thereby causing word duplication.
strtok() always returns a different pointer into the initial string. This cannot produce duplicates, unless you call it twice with the same input string (maybe with new contents).
However, your function returns a pointer to a static array, which is overwritten on each call to split(), voiding the results of all previous calls. To prevent this,
either allocate new memory in each call (which must be freed by the caller):
char *words = calloc(MAX_LENGTH / 2, 1);
or return a struct instead (which is always copied by value):
struct wordlist { char *word[MAX_LENGTH / 2]; };
wordlist split(char *string)
{
wordlist list = {};
/* ... */
list.word[index] = /* ... */;
/* ... */
return list;
}

malloc() for an array of strings, not working as hoping

I am trying to make a function that takes a string and a pointer to an array of strings and malloc() the array of char arrays and copies each individual word of the string. This is what I have so far, I think I'm close, I'm just struggling with using malloc() on an array of arrays.
int string_parser(char *inp, char **array_of_words_p[])
{
int CurrentChar = 0; //Variable Initialization
char *buffer; //Variable Initialization
/* Allocate memory and check for errors allocating memory */
//Allocate memory to buffer the size of the input string
buffer = (char*)malloc(strlen(inp));
if (buffer == NULL)
{
printf("Error allocating memory..\n");
return -1;
}
/* Move input string into buffer before processing */
for (CurrentChar = 0; CurrentChar < strlen(inp) + 1; CurrentChar++)
{ //For every character in input
if (inp != NULL)
{
//Move input character into buffer
buffer[CurrentChar] = inp[CurrentChar];
}
}
/* Convert string into array of words */
char ** stringbuffer = NULL;
//Convert string to array of words
char * CurrentWord = strtok_s(buffer, " ", *array_of_words_p);
//Variable Initialization
int numspaces = 0;
while (CurrentWord)
{
//Allocate memory for size of string
stringbuffer = (char**)realloc(stringbuffer, sizeof(char**) * ++numspaces);
if (stringbuffer == NULL)
{
return -1;
}
stringbuffer[numspaces - 1] = CurrentWord;
//Reset Current word to null
CurrentWord = strtok_s(NULL, " ", *array_of_words_p);
}
//Reallocate memory to include terminating character
stringbuffer = (char**)realloc(stringbuffer, sizeof(char**) * (numspaces + 1));
stringbuffer[numspaces] = 0;
/* Write processed data into returned argument */
*array_of_words_p = (char**)malloc(sizeof(char**) * (numspaces + 2));
memcpy(*array_of_words_p, stringbuffer, (sizeof(char*) * (numspaces + 2)));
free(stringbuffer);
return numspaces;
}
//Allocate memory to buffer the size of the input string
buffer = (char*)malloc(strlen(inp));
The size of the input string includes the terminating \0, so you need:
buffer = malloc(strlen(inp)+1);
//Convert string to array of words
char * CurrentWord = strtok_s(buffer, " ", *array_of_words_p);
It's unwise to abuse the *array_of_words_p for the context save variable, as this requires it to be initialized appropriately. Better:
char *context, *CurrentWord = strtok_s(buffer, " ", &context);
…
CurrentWord = strtok_s(NULL, " ", &context);
//Allocate memory for size of string
stringbuffer = (char**)realloc(stringbuffer, sizeof(char**) * ++numspaces);
It likely doesn't hurt (owing to equal pointer sizes), but sizeof(char**) is strictly speaking wrong, since the elements of the array of strings are of type char *. Correct:
stringbuffer = realloc(stringbuffer, sizeof (char *) * ++numspaces);
…
stringbuffer = realloc(stringbuffer, sizeof (char *) * (numspaces + 1));
/* Write processed data into returned argument */
*array_of_words_p = (char**)malloc(sizeof(char**) * (numspaces + 2));
memcpy(*array_of_words_p, stringbuffer, (sizeof(char*) * (numspaces + 2)));
free(stringbuffer);
You can spare this unnecessary copying and by the way avoid accessing the unallocated memory stringbuffer[numspaces+1] by replacing the above with just:
*array_of_words_p = stringbuffer;
Apart from that all, your function works and can be called like:
char **array_of_words;
int n = string_parser("this here is an example string", &array_of_words);
for (int i = 0; i < n; ++i) puts(array_of_words[i]);

Combining Strings into a List in C

Below is my code for the following issue. I'm trying to take a string of first names and string of last names that are separated by commas and transform them into a list of full names. For example, if firstnames = "John,Jane" and lastnames = "Smith,Doe", then the output should be ["John Smith", "Jane Doe"].
I believe my issue is arising in my use of strtok since first_names[i] = name is giving me an error. Any help on this would be much appreciated!
char **combine_names(char *firstnames, char *lastnames) {
char first_names[50][50];
char last_names[50][50];
int i = 0;
char *name = strtok(firstnames, ",");
while (name != NULL) {
first_names[i] = name;
i++;
name = strtok(NULL, ",");
}
i = 0;
name = strtok(lastnames, ",");
while (name != NULL) {
last_names[i] = name;
i++;
name = strtok(NULL, ",");
}
char **names;
names = malloc(strlen(first_names) * sizeof(char*));
for (int i = 0; i < strlen(first_names); i++) {
names[i] = malloc(51 * sizeof(char));
}
int i = 0;
int j = 0;
int k = 0;
while (first_names[i] != '\0') {
while (first_names[i][j] != '\0') {
names[i][j] = first_names[i][j];
j++;
}
names[i][j] = ' ';
j++;
while (second_names[i][k] != '\0') {
names[i][j] = second_names[i][k];
j++;
k++;
}
names[i][j] = '\0';
i++;
}
names[i] = '\0';
return names;
}
The following line is causing an incompatible pointer error with the first argument. Why is that?
names = malloc(strlen(first_names) * sizeof(char*));
Using strtok() does indeed pose some problems, but the main issue is your allocating names with an invalid expression malloc(strlen(first_names) * sizeof(char*));. first_names is not a C string, strlen(first_names) does not compute the number of entries in the first_names array.
Here is a simpler and safer approach:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char **combine_names(const char *firstnames, const char *lastnames) {
int n = 0;
char **names = malloc(sizeof(*names) * (n + 1));
char *p;
if (names == NULL) {
perror("cannot allocate memory\n");
}
while (*firstnames && *lastnames) {
int len1 = strcspn(firstnames, ",");
int len2 = strcspn(lastnames, ",");
int size = len1 + 1 + len2 + 1;
p = malloc(size);
if (p == NULL) {
perror("cannot allocate memory\n");
}
snprintf(p, size, "%.*s%s%.*s",
len1, firstnames,
len1 && len2 ? " " : "",
len2, lastnames);
names = realloc(names, sizeof(*names) * (n + 2));
if (names == NULL) {
perror("cannot allocate memory\n");
}
names[n++] = p;
firstnames += len1 + (firstnames[len1] == ',');
lastnames += len2 + (lastnames[len2] == ',');
}
names[n] = NULL;
return names;
}
Remember that first_names is a double array of characters. That means that first_names[i] is actually a string, or an array of characters. You can't assign directly to an array of characters, instead you have to write to the array character by character. The easiest way to do this is using string copy or
strcpy(first_names[i], name), but strcpy doesn't protect against buffer overflows. One method is to use strncpy, just be careful because this will not guarantee the string is null-terminated when the source string exceeds the size of the destination string. To fix this, use
strncpy(first_names[i], name, 50);
first_names[i][49] = '\0';
Considering the disadvantages of strncpy it's probably best to use a solution similar to #chqrlie.

Resources