dynamically allocating my 2d array in c - c

Any hints on how I would dynamically allocate myArray so I can enter any amount of strings and it would store correctly.
int main()
{
char myArray[1][1]; //how to dynamically allocate the memory?
counter = 0;
char *readLine;
char *word;
char *rest;
printf("\n enter: ");
ssize_t buffSize = 0;
getline(&readLine, &buffSize, stdin);//get user input
//tokenize the strings
while(word = strtok_r(readLine, " \n", &rest )) {
strcpy(myArray[counter], word);
counter++;
readLine= rest;
}
//print the elements user has entered
int i =0;
for(i = 0;i<counter;i++){
printf("%s ",myArray[i]);
}
printf("\n");
}

Use realloc like this:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void){
char **myArray = NULL;
char *readLine = NULL;
size_t buffSize = 0;
size_t counter = 0;
char *word, *rest, *p;
printf("\n enter: ");
getline(&readLine, &buffSize, stdin);
p = readLine;
while(word = strtok_r(p, " \n", &rest )) {
myArray = realloc(myArray, (counter + 1) * sizeof(*myArray));//check omitted
myArray[counter++] = strdup(word);
p = NULL;
}
free(readLine);
for(int i = 0; i < counter; i++){
printf("<%s> ", myArray[i]);
free(myArray[i]);
}
printf("\n");
free(myArray);
}

Here is one way you might approach this problem. If you are going to dynamically allocate storage for an unknown number of words of unknown length, you can start with a buffSize that seems reasonable, allocate that much space for the readLine buffer, and grow this memory as needed. Similarly, you can choose a reasonable size for the number of words expected, and grow word storage as needed.
In the program below, myArray is a pointer to pointer to char. arrSize is initialized so that pointers to 100 words may be stored in myArray. First, readLine is filled with an input line. If more space than provided by the initial allocation is required, the memory is realloced to be twice as large. After reading in the line, the memory is again realloced to trim it to the size of the line (including space for the '\0').
strtok_r() breaks the line into tokens. The pointer store is used to hold the address of the memory allocated to hold the word, and then word is copied into this memory using strcpy(). If more space is needed to store words, the memory pointed to by myArray is realloced and doubled in size. After all words have been stored, myArray is realloced a final time to trim it to its minimum size.
When doing this much allocation, it is nice to write functions which allocate memory and check for errors, so that you don't have to do this manually every allocation. xmalloc() takes a size_t argument and an error message string. If an allocation error occurs, the message is printed to stderr and the program exits. Otherwise, a pointer to the allocated memory is returned. Similarly, xrealloc() takes a pointer to the memory to be reallocated, a size_t argument, and an error message string. Note here that realloc() can return a NULL pointer if there is an allocation error, so you need to assign the return value to a temporary pointer to avoid a memory leak. Moving realloc() into a separate function helps protect you from this issue. If you assigned the return value of realloc() directly to readLine, for example, and if there were an allocation error, readLine would no longer point to the previously allocated memory, which would be lost. This function prints the error message and exits if there is an error.
Also, you need to free all of these memory allocations, so this is done before the program exits.
This method is more efficient than reallocing memory for every added character in the line, and for every added pointer to a word in myArray. With generous starting values for buffSize and arrSize, you may only need the initial allocations, which are then trimmed to final size. Of course, there are still the individual allocations for each of the individual words. You could also use strdup() for this part, but you would still need to remember to free those allocations as well.Still, not nearly as many allocations will be needed as when readLine and myArray are grown one char or one pointer at a time.
#define _POSIX_C_SOURCE 1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void * xmalloc(size_t size, char *msg);
void * xrealloc(void *ptr, size_t size, char *msg);
int main(void)
{
char **myArray;
size_t buffSize = 1000;
size_t arrSize = 100;
size_t charIndex = 0;
size_t wordIndex = 0;
char *readLine;
char *inLine;
char *word;
char *rest;
char *store;
/* Initial allocations */
readLine = xmalloc(buffSize, "Allocation error: readLine");
myArray = xmalloc(sizeof(*myArray) * arrSize,
"Allocation error: myArray\n");
/* Get user input */
printf("\n enter a line of input:\n");
int c;
while ((c = getchar()) != '\n' && c != EOF) {
if (charIndex + 1 >= buffSize) { // keep room for '\0'
buffSize *= 2;
readLine = xrealloc(readLine, buffSize,
"Error in readLine realloc()\n");
}
readLine[charIndex++] = c;
}
readLine[charIndex] = '\0'; // add '\0' terminator
/* If you must, trim the allocation now */
readLine = xrealloc(readLine, strlen(readLine) + 1,
"Error in readLine trim\n");
/* Tokenize readLine */
inLine = readLine;
while((word = strtok_r(inLine, " \n", &rest)) != NULL) {
store = xmalloc(strlen(word) + 1, "Error in word allocation\n");
strcpy(store, word);
if (wordIndex >= arrSize) {
arrSize *= 2;
myArray = xrealloc(myArray, sizeof(*myArray) * arrSize,
"Error in myArray realloc()\n");
}
myArray[wordIndex] = store;
wordIndex++;
inLine = NULL;
}
/* You can trim this allocation, too */
myArray = xrealloc(myArray, sizeof(*myArray) * wordIndex,
"Error in myArray trim\n");
/* Print words */
for(size_t i = 0; i < wordIndex; i++){
printf("%s ",myArray[i]);
}
printf("\n");
/* Free allocated memory */
for (size_t i = 0; i < wordIndex; i++) {
free(myArray[i]);
}
free(myArray);
free(readLine);
return 0;
}
void * xmalloc(size_t size, char *msg)
{
void *temp = malloc(size);
if (temp == NULL) {
fprintf(stderr, "%s\n", msg);
exit(EXIT_FAILURE);
}
return temp;
}
void * xrealloc(void *ptr, size_t size, char *msg)
{
void *temp = realloc(ptr, size);
if (temp == NULL) {
fprintf(stderr, "%s\n", msg);
exit(EXIT_FAILURE);
}
return temp;
}

I suggest you first scan the data and then call malloc() with the appropriate size.
Otherwise, you can use realloc() to reallocate memory as you go through the data.

Related

Reading the words of a file into a dynamic 2D array

I am trying to read a file and store every word into a dynamically allocated 2D array. The size of the input file is unknown.
I am totally lost and don't know how I could "fix/finish" the program.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
char filename[25];
printf("Input the filename");
scanf("%s", filename);
fileConverter(filename);
}
int fileConverter(char filename[25]) {
//int maxLines = 50000;
//int maxWordSize = 128;
//char words[maxLines][maxWordSize];
//char **words;
char **arr = (char**) calloc(num_elements, sizeof(char*));
for ( i = 0; i < num_elements; i++ ) {
arr[i] = (char*) calloc(num_elements_sub, sizeof(char));
}
FILE *file = NULL;
int amountOfWords = 0;
file = fopen(filename, "r");
if(file == NULL) {
exit(0);
}
while(fgets(words[amountOfWords], 10000, file)) {
words[amountOfWords][strlen(words[amountOfWords]) - 1] = "\0";
amountOfWords++;
}
for(int i = 0; i < amountOfWords; i++) {
printf("a[%d] = ", i);
printf("%s\n", words[i]);
}
printf("The file contains %d words and the same amount of lines.\n", amountOfWords);
return amountOfWords;
The main challenges for this kind of problem are
reallocating the array of strings as the program reads new words, and
handling words that are larger than the buffer used by fgets.
The general approach for these kind of parsing problems, is to design a state machine. The state machine here has two states:
The current character is whitespace. Action: Continue reading whitespace until we reach the end of the buffer, or until we land on a non-whitespace character, in which case we switch to state 2.
The current character is non-whitespace (i.e. a word). Action: Continue reading non-whitespace until we reach the end of the buffer, or until we land on a whitespace character, in which case we copy the word we just read to the array of strings and switch to state 1.
Particularly difficult is the case in which we are in state 2 and reach the end of the buffer. This means that this word spans multiple buffers. To accommodate for this, we deviate slightly from a direct state machine implementation. State 2 is slightly different, depending on if we are reading a new word or continuing one that was started in a previous buffer.
We now keep track of wordSize. If we start reading from the start of a buffer, but wordSize is not 0, then we know we are continuing a previous word and we know what size it was for the realloc we need.
Below is one possible implementation. All the work is done in the wordArrayRead function. Walking through it from the top of the function:
First we declare the variables that we need across lineBuffer reads: an index for the word itself and the length of the word we are currently reading, followed by the declaration of the buffer itself. The outside loop repeatedly reads using fgets until we have exhausted the input.
We start reading at index 0 and stop at the null-terminator. The first if-statement checks if we should be in state 2: either the current character is the start of a word or we were already reading a word.
State 2
The index wordStartIdx stays at the first character of the word (segment) and we walk the wordEndIdx to the end of the word (segment) or to the end of the buffer.
We then check if we need to increase the size of the array of strings. Here we increase it to 2 times + 1 the previous size to avoid frequent reallocations.
We set a boolean value, indication whether we have reached the end of a word. If we have, we need to allocate for and write the null-terminator at the end of the string.
If wordLength == 0 it means we are reading a new word and have to allocate memory for it for the first time. If wordLength != 0, we have to reallocate to append to an existing word.
We copy the word (segment) currently in the lineBuffer to the array of strings.
Now, we do some bookkeeping. If we reached the end of a word, we write the null-terminator, increment the index to point to the next word location and reset wordLength. If this wasn't the case, we only increment the wordLength with the length of the segment we just read. Finally, we update wordStartIdx, which still points to the start of the word, to point to the end of the word, so we can continue iterating over the buffer.
State 1
Having finishing the State 2 processing, we go into State 1 which has only two lines. It simply advances the index until we land at non-whitespace. Note that the null-terminator of the lineBuffer ('\0') does not count as whitespace, so this loop will not continue past the end of the buffer.
After all input has been processed, we shrink the array of strings to the actual size of its data. This "corrects" the allocation policy of increasing the size by 2n+1 each time it wasn't large enough.
#include <assert.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// BUFFER_SIZE must be >1U
#define BUFFER_SIZE 1024U
struct WordArray
{
char **words;
size_t numberOfWords;
};
static struct WordArray wordArrayConstruct(void);
static void wordArrayResize(struct WordArray *wordArray, size_t const newSize);
static void wordArrayDestruct(struct WordArray *wordArray);
static void wordArrayRead(FILE *restrict stream, struct WordArray *wordArray);
static char *reallocStringWrapper(char *restrict str, size_t const newSize);
static void wordArrayPrint(struct WordArray const *wordArray);
int main(void)
{
struct WordArray wordArray = wordArrayConstruct();
wordArrayRead(stdin, &wordArray);
wordArrayPrint(&wordArray);
wordArrayDestruct(&wordArray);
}
static void wordArrayRead(FILE *restrict stream, struct WordArray *wordArray)
{
size_t wordArrayIdx = 0U;
size_t wordLength = 0U;
char lineBuffer[BUFFER_SIZE];
while (fgets(lineBuffer, sizeof lineBuffer, stream) != NULL)
{
size_t wordStartIdx = 0U;
while (lineBuffer[wordStartIdx] != '\0')
{
if (!isspace(lineBuffer[wordStartIdx]) || wordLength != 0U)
{
size_t wordEndIdx = wordStartIdx;
while (!isspace(lineBuffer[wordEndIdx]) && wordEndIdx != BUFFER_SIZE - 1U)
++wordEndIdx;
if (wordArrayIdx >= wordArray->numberOfWords)
wordArrayResize(wordArray, wordArray->numberOfWords * 2U + 1U);
size_t wordSegmentLength = wordEndIdx - wordStartIdx;
size_t foundWordEnd = wordEndIdx != BUFFER_SIZE - 1U; // 0 or 1 bool
// Allocate for a new word, or reallocate for an existing word
// If a word end was found, add 1 to the size for the '\0' character
char *dest = wordLength == 0U ? NULL : wordArray->words[wordArrayIdx];
size_t allocSize = wordLength + wordSegmentLength + foundWordEnd;
wordArray->words[wordArrayIdx] = reallocStringWrapper(dest, allocSize);
memcpy(&(wordArray->words[wordArrayIdx][wordLength]),
&lineBuffer[wordStartIdx], wordSegmentLength);
if (foundWordEnd)
{
wordArray->words[wordArrayIdx][wordLength + wordSegmentLength] = '\0';
++wordArrayIdx;
wordLength = 0U;
}
else
{
wordLength += wordSegmentLength;
}
wordStartIdx = wordEndIdx;
}
while (isspace(lineBuffer[wordStartIdx]))
++wordStartIdx;
}
}
// All done. Shrink the words array to the size of the actual data
if (wordArray->numberOfWords != 0U)
wordArrayResize(wordArray, wordArrayIdx);
}
static struct WordArray wordArrayConstruct(void)
{
return (struct WordArray) {.words = NULL, .numberOfWords = 0U};
}
static void wordArrayResize(struct WordArray *wordArray, size_t const newSize)
{
assert(newSize > 0U);
char **tmp = (char**) realloc(wordArray->words, newSize * sizeof *wordArray->words);
if (tmp == NULL)
{
wordArrayDestruct(wordArray);
fprintf(stderr, "WordArray allocation error\n");
exit(EXIT_FAILURE);
}
wordArray->words = tmp;
wordArray->numberOfWords = newSize;
}
static void wordArrayDestruct(struct WordArray *wordArray)
{
for (size_t wordStartIdx = 0U; wordStartIdx < wordArray->numberOfWords; ++wordStartIdx)
{
free(wordArray->words[wordStartIdx]);
wordArray->words[wordStartIdx] = NULL;
}
free(wordArray->words);
}
static char *reallocStringWrapper(char *restrict str, size_t const newSize)
{
char *tmp = (char*) realloc(str, newSize);
if (tmp == NULL)
{
free(str);
fprintf(stderr, "Realloc string allocation error\n");
exit(EXIT_FAILURE);
}
return tmp;
}
static void wordArrayPrint(struct WordArray const *wordArray)
{
for (size_t wordStartIdx = 0U; wordStartIdx < wordArray->numberOfWords; ++wordStartIdx)
printf("%zu: %s\n", wordStartIdx, wordArray->words[wordStartIdx]);
}
Note: This program reads input from stdin, as Unix/Linux utilities typically do. Use input redirection to read from a file, or provide a file descriptor to the readWordArray function.
to allocate dynamic 2D array you need:
void allocChar2Darray(size_t rows, size_t columns, char (**array)[columns])
{
*array = malloc(rows * sizeof(**array));
}

Allocating memory for a string in C using malloc

I am trying to allocate memory for an array of strings using malloc. The size of each string is not known before the input from the user, so this is how I tried to allocate memory for each element in the array.
I have some errors with the code, but can't figure them out or can't understand them. I am getting an error regarding the allocation. Can anyone tell me what's wrong about this?
bool read_strings(char * strings[], int n)
{
int i = 0;
while (i<n)
{
char string[MAX_LENGTH];
if (scanf("%s", string)!=1)
return false;
char* memory= (char*)malloc(sizeof(char)*strlen(string));
if (memory == NULL)
return false;
memory = string;
strings[i] = memory;
i++;
}
return true;
}
Thanks a lot!
At least you have to replace
char* memory= (char*)malloc(sizeof(char)*strlen(string));
if (memory == NULL)
return false;
memory = string;
strings[i] = memory;
by
strings[i] = strdup(string)
Note that using scanf("%s", string) the separator between the read string is the space
The problem is right here:
char* memory = (char*)malloc(sizeof(char)*strlen(string));
memory = string; <<<
strings[i] = memory;
You will lose memory if you assign strings to pointers like this.
Either:
a) Copy the string into the newly allocated memory using strcpy() or strncpy(), also make sure you have enough space for the NULL character \0
strings[i] = (char*)malloc(sizeof(char) * (strlen(string) + 1));
strcpy(strings[i], string);
b) Use strdup(), which is like a mix between strcpy() and malloc(), it creates just enough space for your string and copies it into a new memory location
strings[i] = strdup(string);
You have many mistakes
(char*)malloc(sizeof(char)*(strlen(string) **+ 1**)) . You have to reserve memory to '\0'
Very wrong
memory = string;
To copy strings you have to usr strcpy (the correct function nowadays is strncpy is more safe)
To have a truly unlimited buffer in C, (or limited by the amount of memory and a size_t,) you could build up the memory allocation incrementally.
#include <stdlib.h> /* realloc free */
#include <stdio.h> /* stdin fgets printf */
#include <string.h> /* strcpy */
#include <assert.h> /* assert */
#include <stdint.h> /* C99 SIZE_MAX */
#include <stdbool.h> /* C99 bool */
/* Returns an entire line or a null pointer, in which case eof or errno may be
set. If not-null, it must be freed. */
static char *line(void) {
char temp[1024] = "", *str = 0, *str_new;
size_t temp_len, str_len = 0;
while(fgets(temp, sizeof temp, stdin)) {
/* Count the chars in temp. */
temp_len = strlen(temp);
assert(temp_len > 0 && temp_len < sizeof temp);
/* Allocate bigger buffer. */
if(!(str_new = realloc(str, str_len + temp_len + 1)))
{ free(str); return 0; }
str = str_new;
/* Copy the chars into str. */
strcpy(str + str_len, temp);
assert(str_len < SIZE_MAX - temp_len); /* SIZE_MAX >= 65535 */
str_len += temp_len;
/* If on end of line. */
if(temp_len < sizeof temp - 1 || str[str_len - 1] == '\n') break;
}
return str;
}
static bool read_strings(char * strings[], int n) {
char *a;
int i = 0;
while(i < n) {
if(!(a = line())) return false;
strings[i++] = a;
}
return true;
}
int main(void) {
char *strings[4] = { 0 }; /* C99 */
size_t i;
bool success = false;
do {
if(!read_strings(strings, sizeof strings / sizeof *strings)) break;
for(i = 0; i < sizeof strings / sizeof *strings; i++)
printf("%lu: <%s>\n", (unsigned long)i, strings[i]);
success = true;
} while(0); {
for(i = 0; i < sizeof strings / sizeof *strings; i++)
free(strings[i]);
}
return success ? EXIT_SUCCESS : (perror("stdin"), EXIT_FAILURE);
}
I think that's right-ish. However, this should bring pause; what if they never hit enter? If one has a MAX_LENGTH, then consider allocating statically, depending on your situation.
Edit: It also has a worst-case running time that may not be desirable; if entering really arbitrarily large lines, use a geometric progression to allocate space.
I think this is what you wanted to do:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int read_strings(char * strings[], int n)
{
int i = 0;
char buffer[256] ={0}; /*a temp buffer of fixed max size for input */
if(NULL == strings)
{
return 0 ;
}
for (i= 0; i<n; ++i)
{
if (fgets(buffer, 256,stdin)== NULL) /*safer then scanf - read input into the buffer*/
return 0;
strings[i]= malloc(sizeof(char)*(strlen(buffer)+1)); /* the char poiner in he i place will now point to the newly allocated memory*/
strcpy(strings[i], buffer); /*copy the new string into the allocated memory now string[i] is pointing to a string*/
}
return 1;
}
static void printStringsArray(const char* strArr[], size_t size)
{
int i = 0;
if(NULL == strArr)
{
return;
}
for(i = 0; i< size; ++i)
{
printf("%s", strArr[i]);
}
}
int main(void)
{
char * arr[3]; /*array of (char*) each will point to a string after sending it to the function */
read_strings(arr,3);
printStringsArray(arr,3);
return 0;
}

Error when entering a words in array of pointers to char

This program checks if words in array of pointers to char **s(that you should load first) are the same as words in function fun. For some reason, I can't load words properly in the main function and I don't know where why.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int fun(char **s,int j)
{
int i, num=0;
for(i=0;i<j;i++)
{
if(strcmp(s[i],"first")==0)
num++;
if(strcmp(s[i],"second")==0)
num++;
}
if(num==j)
return 1;
else
return 0;
}
int main(int argc,char *argv[])
{
char **s=(char**)malloc(20*sizeof(char*)); // alloc
char ss[20];
int i=0,ret;
while(1) // while string 'ss' is different from string "stop"
{
scanf("%s",ss);
if(strcmp(ss,"stop")==0)
break;
else
{ s[i]=ss; // if ss is different from "stop"
i++;
}
}
ret=fun(s,i); // returns 1 if words are the same as words in function fun
if(ret)
printf("Matching\n");
else
printf("Doesn't matches\n");
for(int t=0;t<i;t++)
free(s[i]);
free(s);
}
char **s=(char**)malloc(20*sizeof(char*)); // alloc
That makes room for 20 pointers to char. It doesn't make any room to actually store the chars.
s[i]=ss; // if ss is different from "stop"
Doing that in your loop will just result in a bunch of pointers to the same underlying sequence of chars. You need to use either a loop or a function like strncpy to actually copy the chars.
for(int t=0;t<i;t++)
free(s[i]);
This should be an obvious sign of a mistake, as there's no malloc corresponding with this free. (Your only malloc corresponds with the free(s); below this.)
Following all your instructions, I solved it using function strcpy for those who maybe has the same problem:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int fun(char **s,int j)
{
int i;
int num=0,numm=0;
for(i=0;i<j;i++)
{
if(strcmp(s[i],"first")==0)
num=1;
if(strcmp(s[i],"second")==0)
numm=1;
}
if(num!=0 && numm!=0)
return 1;
else
return 0;
}
int main(int argc,char *argv[])
{
char **s; // pointer to pointer to char
char ss[20];
int i=0,ret; // i-number of words we entered
scanf("%s",ss);
if(strcmp(ss,"stop")!=0)
{
s=(char**)malloc(1*sizeof(char*));
s[i]=malloc(20*sizeof(char));
strcpy(s[i],ss);
i++;
}
while(1) // while string 'ss' is different from string "stop"
{
scanf("%s",ss);
if(strcmp(ss,"stop")!=0)
{
s=(char**)realloc(s,(i+1)*sizeof(char*));
s[i]=malloc(20*sizeof(char));
strcpy(s[i],ss);
i++;
}
else
break;
}
ret=fun(s,i); // returns 1 if words are the same as words in function fun
if(ret)
printf("\nMatching\n");
else
printf("\nDoesn't matches\n");
for(int t=0;t<i;t++)
free(s[i]);
free(s);
}
Simple solution, use strdup if you have it
Your problem was you were only allocating 20 pointers. The pointers you allocate are uninitialized and do not point to any valid storage themselves. In order to store strings at each s[i] pointer, you must allocate storage sufficient to store ss (strlen(ss) + 1 chars) and then copy ss to the new block of memory you allocated. If you have the strdup function, that will both allocate and copy for you, but since it allocates, it is up to you to validate the allocation succeeded, e.g.
#define NPTR 20 /* if you need a constant, #define one (or more) */
int main (int argc, char *argv[])
{
char **s = NULL; /* initialize all variables */
char ss[20] = "";
int i = 0,
ret = 0;
s = malloc (NPTR * sizeof *s); /* allocate NPTR pointers to char */
if (s == NULL) { /* validate EVERY allocation */
perror ("malloc-s");
return 1;
}
/* fill up to NPTR pointers with strings read from stdin */
while (i < NPTR && scanf ("%19s", ss) == 1) {
/* compare for stop */
if (strcmp (ss, "stop") == 0)
break;
/* strdup will allocate for s[i] and copy ss */
s[i] = strdup (ss);
if (s[i] == NULL) { /* it allocates -> you must validate */
perror ("strdup-s[i]");
break;
}
i++;
}
}
(note: you must protect your array bounds for ss by including the filed width modifier in your scanf call, e.g. scanf ("%19s", ss))
If you don't have strdup, just malloc and strcpy (or better memcpy)
Without strdup, you simply get the length of the string in ss, allocate length+1 chars assinging the starting address for the new block of memory to s[i] and then copy ss to the new block of memory. The only change to above would be:
...
/* fill up to NPTR pointers with strings read from stdin */
while (i < NPTR && scanf ("%19s", ss) == 1) {
size_t len = 0;
/* compare for stop */
if (strcmp (ss, "stop") == 0)
break;
len = strlen (ss); /* get length of string in ss */
s[i] = malloc (len + 1); /* allocate length + 1 chars */
if (s[i] == NULL) { /* you allocate / you validate */
perror ("malloc-s[i]");
break;
}
memcpy (s[i], ss, len + 1); /* you have length, just memcpy */
i++;
}
...
(note: above, you have already scanned ss to with strlen to get the length, so there is no need to strcpy (which will scan for the end again), just use memcpy with the length you have +1 to copy the nul-terminating character which will be slightly more efficient).
Either way works. Then to free the memory you have allocated.
for (int j = 0; j < i; j++)
free (s[j]); /* free storage for each string */
free (s); /* free pointers */
(note also: the i < NPTR check to prevent storing more strings than you have pointers allocated. Instead of stopping at i == NPTR, you can simply realloc the number of pointers and continue on. The general approach is to keep track of the number of pointers you have allocated, and when you reach your limit, realloc to double the number of pointers (or multiply current number by 3/2, or whatever you like), validate the reallocation succeeded, and update the current number allocated to the new number of pointers and keep going until you hit your pointer limit again, and repeat as required...)

Scanf unknown number of string arguments C

I wanted to know if there was a way to use scanf so I can take in an unknown number of string arguments and put them into a char* array. I have seen it being done with int values, but can't find a way for it to be done with char arrays. Also the arguments are entered on the same line separated by spaces.
Example:
user enters hello goodbye yes, hello gets stored in array[0], goodbye in array[1] and yes in array[2]. Or the user could just enter hello and then the only thing in the array would be hello.
I do not really have any code to post, as I have no real idea how to do this.
You can do something like, read until the "\n" :
scanf("%[^\n]",buffer);
you need to allocate before hand a big enough buffer.
Now go through the buffer count the number of words, and allocate the necessary space char **array = ....(dynamic string allocation), go to the buffer and copy string by string into the array.
An example:
int words = 1;
char buffer[128];
int result = scanf("%127[^\n]",buffer);
if(result > 0)
{
char **array;
for(int i = 0; buffer[i]!='\0'; i++)
{
if(buffer[i]==' ' || buffer[i]=='\n' || buffer[i]=='\t')
{
words++;
}
}
array = malloc(words * sizeof(char*));
// Using RoadRunner suggestion
array[0] = strtok (buffer," ");
for(int w = 1; w < words; w++)
{
array[w] = strtok (NULL," ");
}
}
As mention in the comments you should use (if you can) fgets instead fgets(buffer,128,stdin);.
More about strtok
If you have an upper bound to the number of strings you may receive from the user, and to the number of characters in each string, and all strings are entered on a single line, you can do this with the following steps:
read the full line with fgets(),
parse the line with sscanf() with a format string with the maximum number of %s conversion specifiers.
Here is an example for up to 10 strings, each up to 32 characters:
char buf[400];
char s[10][32 + 1];
int n = 0;
if (fgets(buf, sizeof buf, sdtin)) {
n = sscanf("%32s%32s%32s%32s%32s%32s%32s%32s%32s%32s",
s[0], s[1], s[2], s[3], s[4], s[5], s[6], s[7], s[8], s[9]));
}
// `n` contains the number of strings
// s[0], s[1]... contain the strings
If the maximum number is not known of if the maximum length of a single string is not fixed, or if the strings can be input on successive lines, you will need to iterate with a simple loop:
char buf[200];
char **s = NULL;
int n;
while (scanf("%199s", buf) == 1) {
char **s1 = realloc(s, (n + 1) * sizeof(*s));
if (s1 == NULL || (s1[n] = strdup(buf)) == NULL) {
printf("allocation error");
exit(1);
}
s = s1;
n++;
}
// `n` contains the number of strings
// s[0], s[1]... contain pointers to the strings
Aside from the error handling, this loop is comparable to the hard-coded example above but it still has a maximum length for each string. Unless you can use a scanf() extension to allocate the strings automatically (%as on GNU systems), the code will be more complicated to handle any number of strings with any possible length.
You can use:
fgets to read input from user. You have an easier time using this instead of scanf.
malloc to allocate memory for pointers on the heap. You can use a starting size, like in this example:
size_t currsize = 10
char **strings = malloc(currsize * sizeof(*strings)); /* always check
return value */
and when space is exceeded, then realloc more space as needed:
currsize *= 2;
strings = realloc(strings, currsize * sizeof(*strings)); /* always check
return value */
When finished using the requested memory from malloc() and realloc(), it's always to good to free the pointers at the end.
strtok to parse the input at every space. When copying over the char * pointer from strtok(), you must also allocate space for strings[i], using malloc() or strdup.
Here is an example I wrote a while ago which does something very similar to what you want:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define INITSIZE 10
#define BUFFSIZE 100
int
main(void) {
char **strings;
size_t currsize = INITSIZE, str_count = 0, slen;
char buffer[BUFFSIZE];
char *word;
const char *delim = " ";
int i;
/* Allocate initial space for array */
strings = malloc(currsize * sizeof(*strings));
if(!strings) {
printf("Issue allocating memory for array of strings.\n");
exit(EXIT_FAILURE);
}
printf("Enter some words(Press enter again to end): ");
while (fgets(buffer, BUFFSIZE, stdin) != NULL && strlen(buffer) > 1) {
/* grow array as needed */
if (currsize == str_count) {
currsize *= 2;
strings = realloc(strings, currsize * sizeof(*strings));
if(!strings) {
printf("Issue reallocating memory for array of strings.\n");
exit(EXIT_FAILURE);
}
}
/* Remove newline from fgets(), and check for buffer overflow */
slen = strlen(buffer);
if (slen > 0) {
if (buffer[slen-1] == '\n') {
buffer[slen-1] = '\0';
} else {
printf("Exceeded buffer length of %d.\n", BUFFSIZE);
exit(EXIT_FAILURE);
}
}
/* Parsing of words from stdin */
word = strtok(buffer, delim);
while (word != NULL) {
/* allocate space for one word, including nullbyte */
strings[str_count] = malloc(strlen(word)+1);
if (!strings[str_count]) {
printf("Issue allocating space for word.\n");
exit(EXIT_FAILURE);
}
/* copy strings into array */
strcpy(strings[str_count], word);
str_count++;
word = strtok(NULL, delim);
}
}
/* print and free strings */
printf("Your array of strings:\n");
for (i = 0; i < str_count; i++) {
printf("strings[%d] = %s\n", i, strings[i]);
free(strings[i]);
strings[i] = NULL;
}
free(strings);
strings = NULL;
return 0;
}

C pointers inconsistency

I'm working on a program for a C class and I reached a point where I don't know what to do. We are implementing a String library type.
I have my header file (MyString.h)
typedef struct {
char *buffer;
int length;
int maxLength;
} String;
String *newString(const char *str);
The file implementing the functions (MyString.c)
#include <stdlib.h>
#include <stdio.h>
#include "MyString.h"
String *newString(const char *str) {
// Allocate memory for the String
String *newStr = (String*)malloc(sizeof(String));
if (newStr == NULL) {
printf("ERROR: Out of memory\n");
return NULL;
}
// Count the number of characters
int count;
for (count = 0; *(str + count) != '\0'; count++);
count++;
// Allocate memory for the buffer
newStr->buffer = (char*)malloc(count * sizeof(char));
if (newStr->buffer == NULL) {
printf("ERROR: Out of memory\n");
return NULL;
}
// Copy into the buffer
while (*str != '\0')
*(newStr->buffer++) = *(str++);
*(++newStr->buffer) = '\0';
// Set the length and maximum length
newStr->length = count;
newStr->maxLength = count;
printf("newStr->buffer: %p\n",newStr->buffer); // For testing purposes
return newStr;
}
And a tester (main.c)
#include <stdio.h>
#include "MyString.h"
main() {
char str[] = "Test character array";
String *testString = newString(str);
printf("testString->buffer: %p\n",testString->buffer); // Testing only
}
The problem is that, even though testString is pointing to the String created in newString(), their buffers point to different memory addresses. Why is that?
Thanks in advance
By using *(++newStr->buffer) and *(newStr->buffer++), you're moving newStr->buffer to essentially point to the end of the string.. You need to modify your code as such:
#include <stdlib.h>
#include <stdio.h>
#include "MyString.h"
String *newString(const char *str) {
// Allocate memory for the String
String *newStr = (String*)malloc(sizeof(String));
if (newStr == NULL) {
printf("ERROR: Out of memory\n");
return NULL;
}
// Count the number of characters
int count;
for (count = 0; *(str + count) != '\0'; count++);
count++;
// Allocate memory for the buffer
newStr->buffer = (char*)malloc(count * sizeof(char));
if (newStr->buffer == NULL) {
printf("ERROR: Out of memory\n");
return NULL;
}
char *pBuffer = newStr->buffer; // don't move newStr->buffer, have another pointer for that.
// Copy into the buffer
while (*str != '\0')
*(pBuffer++) = *(str++);
*pBuffer = '\0';
// Set the length and maximum length
newStr->length = count;
newStr->maxLength = count;
printf("newStr->buffer: %p\n", newStr->buffer); // For testing purposes
return newStr;
}
As the other colleagues already pointed out, you modified your allocation pointer, which is a no no. Here your example but translated to a more "professional" way.
I would change your structure to:
typedef struct {
char *buffer;
size_t length; /* strings and allocation in C are of type size_t not int */
size_t alloclength;
} String;
String *newString(const char *str);
And the function would be changed to.
#include <stdlib.h>
#include <stdio.h>
#include "MyString.h"
String *newString(const char *str)
{
// Allocate memory for the String
String *newStr = malloc(sizeof (String)); /* No typecast of void * in C, it's bad taste. */
if(!newStr) {
fprintf(stderr, "ERROR: Out of memory\n"); /* Errors are meant to be printed on stderr, not stdio */
return NULL;
}
// Count the number of characters
newStr->length = strlen(str); /* Learn to use the stdlib, there are a lot of usefull functions */
newStr->alloclength = newStr->length+1;
// Allocate memory for the buffer
newStr->buffer = malloc(newStr->alloclength); /* sizeof (char) is by definition always 1 */
if(!newStr->buffer) {
fprintf(stderr, "ERROR: Out of memory\n");
free(newStr);
return NULL;
}
// Copy into the buffer
strcpy(newStr->buffer, str); /* Because we already scaned the input with strlen, we can use safely the "unsafe" strcpy function. The strcpy will add the trailing 0 */
printf("newStr->buffer: %p\n",newStr->buffer); // For testing purposes
return newStr;
}
You are modifying the buffer pointer inside the newly created String struct.
You should do:
char *newBuffer = newStr->buffer;
// Copy into the buffer
while (*str != '\0')
*(newBuffer++) = *(str++);
*(++newBuffer) = '\0';
The question is answered, but I think that there is a piece of code that you should add to avoid a subtle source of memory leak:
// Allocate memory for the buffer
newStr->buffer = (char*)malloc(count * sizeof(char));
if (newStr->buffer == NULL) {
printf("ERROR: Out of memory\n");
free(newStr); // free the memory allocated for newStr
return NULL;
}
The explanation is pretty simple: You are modifying the buffer pointer in the newString() function:
// Copy into the buffer
while (*str != '\0')
*(newStr->buffer++) = *(str++);
*(++newStr->buffer) = '\0';
You could use a temporary pointer here (like suggested in the other answers), but I'd like to recommend using the standard functions provided within string.h:
// Count the number of characters
int count;
count = strlen(str) + 1;
// Copy into the buffer
memcpy(newString->buffer, str, count)

Resources