Why my program skips a for cicle? C - c

im trying to code a function that splits a string onto multiple ones, i know I have alot of allocated space not freed, I'm just testing this bit but valgrind displays me
Conditional jump or move depends on uninitialised value(s)
==25613== at 0x4C2DB3C: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==25613== by 0x40090C: split (strutil.c:32)
==25613== by 0x400A00: main (strutil.c:45)
==25613== Uninitialised value was created by a stack allocation
==25613== at 0x400720: split (strutil.c:9)
a few similar errors and then sigsem and closes. my concern is that when i run it with gdb in that for circle thats supposed to count the ',' it circles as expected until it reaches the value ',' then skips entire cicle but the i++ and keeps going. Why does it do that? I watched over gdb and all the parameters (str[i] , sep) have the correct values inside at the moment before the conditional.
#include "strutil.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
char** split(const char* str, char sep){
size_t cant = 2;
size_t i;
for(i = 0; i < strlen(str); i++){//this is line 9
if(str[i] == sep)
cant ++;
i++;
}
size_t corte[cant];
i = 0;
corte[0] = 0;
size_t j = 1;
size_t cant_corte[cant];
for(i = 0; i < strlen(str); i++){
if(str[i] == sep){
corte[j] = i + 1;
cant_corte[j - 1] = corte[j] - corte[j - 1];
//printf("pasa\n");
j++;
}
printf("pasa\n");
i++;
}
char** strv = malloc(sizeof(char*) * cant);
if (strv == NULL)return NULL;
for(i=0; i < cant; i++){
strv[i] = malloc(sizeof(char) * cant_corte[i]);
if (strv[i] == NULL)return NULL;
strncpy(strv[i], str + corte[i], cant_corte[i-1]);
strcat(strv[i], "\0");
}
strv[cant + 2] = NULL;
return strv;
}
int main(){
char* eje = "abc,defg";
printf("%s\n", eje);
char r = ',';
char** prueba = split(eje, r);
printf("%s\n", prueba[0]);
getchar();
return 0;
}

If you do not want your i variable to continue to increment, then make sure in your for loop you do not increment i, and instead depend on the for loop to do its' job. ( increment i every time the condition is true ).

Related

Printing array of strings produces bad output

I'm trying to solve a challenge, but I have no idea of what's wrong with my code!
The challenge is:
Create a function that splits a string of characters into words.
Separators are spaces, tabs and line breaks.
This function returns an array where each box contains a character-string’s address represented by a word. The last element of this array should be equal to 0 to emphasise the end of the array.
There can’t be any empty strings in your array. Draw the necessary conclusions.
The given string can’t be modified.
Note: The only allowed function is malloc()
The bug/problem:
I faced this problem and I tried to solve it but I wasn't able to identify what's wrong.
I created a function named split_whitespaces() to do the job.
When I print the array of strings inside of the split_whitespaces function, I get the following output:
Inside the function:
arr_str[0] = This
arr_str[1] = is
arr_str[2] = just
arr_str[3] = a
arr_str[4] = test!
And when I print the array of string inside the main function, I get the following output:
Inside the main function:
arr_str[0] = #X#?~
arr_str[1] = `X#?~
arr_str[2] = just
arr_str[3] = a
arr_str[4] = test!
I created a function word_count to count how many words in the input string so I can allocate memory using malloc and with word_count + 1 (null pointer).
int word_count(char *str) {
int i;
int w_count;
int state;
i = 0;
w_count = 0;
state = 0;
while (str[i]) {
if (!iswhitespace(str[i])) {
if (!state)
w_count++;
state = 1;
i++;
} else {
state = 0;
i++;
}
}
return (w_count);
}
And another function called strdup_w to mimic the behavior of strdup but just for single words:
char *strdup_w(char *str, int *index) {
char *word;
int len;
int i;
i = *index;
len = 0;
while (str[i] && !iswhitespace(str[i]))
len++, i++;;
word = (char *) malloc(len + 1);
if (!word)
return (NULL);
i = 0;
while (str[*index]) {
if (!iswhitespace(str[*index])) {
word[i++] = str[*index];
(*index)++;
} else
break;
}
word[len] = '\0';
return (word);
}
Here's my full code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char **split_whitespaces(char *str);
char *strdup_w(char *str, int *index);
int word_count(char *str);
int iswhitespace(char c);
int main(void) {
char *str = "This is just a test!";
char **arr_str;
int i;
i = 0;
arr_str = split_whitespaces(str);
printf("\nOutside the function:\n");
while (arr_str[i]) {
printf("arr_str[%d] = %s\n", i, arr_str[i]);
i++;
}
return (0);
}
char **split_whitespaces(char *str) {
char **arr_str;
int i;
int words;
int w_i;
i = 0;
w_i = 0;
words = word_count(str);
arr_str = (char **)malloc(words + 1);
if (!arr_str)
return (NULL);
printf("Inside the function:\n");
while (w_i < words) {
while (iswhitespace(str[i]) && str[i])
if (!str[i++])
break;
arr_str[w_i] = strdup_w(str, &i);
printf("arr_str[%d] = %s\n", w_i, arr_str[w_i]);
w_i++;
}
arr_str[words] = 0;
return (arr_str);
}
char *strdup_w(char *str, int *index) {
char *word;
int len;
int i;
i = *index;
len = 0;
while (str[i] && !iswhitespace(str[i]))
len++, i++;;
word = (char *)malloc(len + 1);
if (!word)
return (NULL);
i = 0;
while (str[*index]) {
if (!iswhitespace(str[*index])) {
word[i++] = str[*index];
(*index)++;
} else
break;
}
word[len] = '\0';
return (word);
}
int word_count(char *str) {
int i;
int w_count;
int state;
i = 0;
w_count = 0;
state = 0;
while (str[i]) {
if (!iswhitespace(str[i])) {
if (!state)
w_count++;
state = 1;
i++;
} else {
state = 0;
i++;
}
}
return (w_count);
}
int iswhitespace(char c) {
if (c == ' ' || c == '\t' || c == '\n' || c == '\r')
return (1);
return (0);
}
I'm sorry, if anything this is my first time trying to seek help.
There are multiple problems in the code:
the size is incorrect in arr_str = (char **)malloc(words + 1); You must multiply the number of elements by the size of the element:
arr_str = malloc(sizeof(*arr_str) * (words + 1));
it is good style to free the array in the main() function after use.
the test while (iswhitespace(str[i]) && str[i]) is redundant: if w_count is computed correctly, testing str[i] should not be necessary. You should use strspn() to skip the white space and strcspn() to skip the word characters.
if (!str[i++]) break; is completely redundant inside the loop: str[i] has already been tested and is not null.
while (str[i] && !iswhitespace(str[i])) len++, i++;; is bad style. Use braces if there is more than a single simple statement in the loop body.
the last loop in strdup_w is complicated, you could simply use memcpy(word, str + *index, len); *index += len;
Here is a modified version:
#include <stdio.h>
#include <stdlib.h>
char **split_whitespaces(const char *str);
char *strdup_w(const char *str, int *index);
int word_count(const char *str);
int iswhitespace(char c);
int main(void) {
const char *str = "This is just a test!";
char **arr_str;
int i;
arr_str = split_whitespaces(str);
if (arr_str) {
printf("\nOutside the function:\n");
i = 0;
while (arr_str[i]) {
printf("arr_str[%d] = %s\n", i, arr_str[i]);
i++;
}
while (i --> 0) {
free(arr_str[i]);
}
free(arr_str);
}
return 0;
}
char **split_whitespaces(const char *str) {
char **arr_str;
int i;
int words;
int w_i;
i = 0;
w_i = 0;
words = word_count(str);
arr_str = malloc(sizeof(*arr_str) * (words + 1));
if (!arr_str)
return NULL;
printf("Inside the function:\n");
while (w_i < words) {
while (iswhitespace(str[i]))
i++;
arr_str[w_i] = strdup_w(str, &i);
if (!arr_str[w_i])
break;
printf("arr_str[%d] = %s\n", w_i, arr_str[w_i]);
w_i++;
}
arr_str[words] = NULL;
return arr_str;
}
char *strdup_w(const char *str, int *index) {
char *word;
int len;
int start;
int i;
i = *index;
start = i;
while (str[i] && !iswhitespace(str[i])) {
i++;
}
*index = i;
len = i - start;
word = malloc(len + 1);
if (!word)
return NULL;
i = 0;
while (i < len) {
word[i] = str[start + i];
i++;
}
word[i] = '\0';
return word;
}
int word_count(const char *str) {
int i;
int w_count;
int state;
i = 0;
w_count = 0;
state = 0;
while (str[i]) {
if (!iswhitespace(str[i])) {
if (!state)
w_count++;
state = 1;
} else {
state = 0;
}
i++;
}
return w_count;
}
int iswhitespace(char c) {
return (c == ' ' || c == '\t' || c == '\n' || c == '\r');
}
From my top comment ...
In split_whitespaces, try changing:
arr_str = (char **) malloc(words + 1);
into:
arr_str = malloc(sizeof(*arr_str) * (words + 1));
As you have it, words is a count and not a byte length, so you're not allocating enough space, so you have UB.
UPDATE:
But watched some tutorials and they said that malloc takes one argument which is the size of the memory to be allocated (in bytes), that's why I allocated memory for 5 bytes! can you please tell my an alternative of using malloc without sizeof() function. I'll appreciate it. – Achraf EL Khnissi
There's really no clean way to specify this without sizeof.
sizeof is not a function [despite the syntax]. It is a compiler directive. It "returns" the number of bytes occupied by its argument as a compile time constant.
If we have char buf[5];, there are 5 bytes, so sizeof(buf) [or sizeof buf] is 5.
If we have: int buf[5];, there are 5 elements, each of size int which is [typically] 4 bytes, so the total space, in bytes, is sizeof(int) * 5 or 4 * 5 which is 20.
But, int can vary depending on the architecture. On Intel 8086's [circa the 1980's], an int was 2 bytes (i.e. 16 bits). So, the above 4 * 5 would be wrong. It should be 2 * 5.
If we use sizeof(int), then sizeof(int) * 5 works regardless of the architecture.
Similarly, on 32 bit machines, a pointer is [usually] 32 bits. So, sizeof(char *) is 4 [bytes]. On a 64 bit machine, a pointer is 64 bits, which is 8 bytes. So, sizeof(char *) is 8.
Because arr_str is: char **arr_str, we could have done:
arr_str = malloc(sizeof(char *) * (words + 1));
But, if the definition of arr_str ever changed (to (e.g.) struct string *arr_str;), then what we just did would break/fail if we forgot to change the assignment to:
arr_str = malloc(sizeof(struct string) * (words + 1));
So, doing:
arr_str = malloc(sizeof(*arr_str) * (words + 1));
is a preferred idiomatic way to write cleaner code. More statements will adjust automatically without having to find all affected lines of code manually.
UPDATE #2:
You might just add why you removed the (char **) cast :) -- chqrlie
Note that I removed the (char **) cast. See: Do I cast the result of malloc?
This just adds extra/unnecessary "stuff" as the void * return value of malloc can be assigned to any type of pointer.
If we forgot to do: #include <stdlib.h>, there would be no function prototype for malloc, so the compiler would default the return type to int.
Without the cast, the compiler would issue an an error on the statement [which is what we want].
With the cast, this action is masked at compile time [more or less]. On a 64 bit machine, the compiler will use a value that is truncated to 32 bits [because it thinks malloc returns a 32 bit value] instead of the full 64 bit return value of malloc.
This truncation is a "silent killer". What should have been flagged as a compile time error produces a runtime fault (probably segfault or other UB) that is much harder to debug.

c language How I free a pointer after I move it

what I wanna to do is get space separated words into a 2d array.what I am thinking is replace space by a '\0',so I can copy the string to array.And then add the string pointer to the place after '\0'.keeping do it till the last.But when I done it ,I consider how can I free the pointer.I consider maybe I can save it first so I use char *t = s.But when I free the t I get a segment error.so how should i free the s pointer after it move to another place.
beside this,I also have some questions:
1.after I malloc a sizeof(char)*15 memory ,I expect printf strlen(s) I can print 15,but I get 0,why?If I wanna know the size of s now,how should I do?
2.after strcpy str to s,the strlen s become 11.But I malloc size of 15,where will the left memory go? does that effect program?
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(){
char buf[10][5];
char *str = "10 8 6 9 76";
char *s;
char *t = s;
s = (char *)malloc(sizeof(char)*15);
printf("strlen 1 s:%d\n",strlen(s));
memset(s,0x00,sizeof(char)*15);
printf("strlen 2 s:%d\n",strlen(s));
strcpy(s,str);
int n = strlen(s);
int i = 0;
int j = 0;
int k = 0;
for( i = 0; i < n; i++ ){
if( s[i] == ' '){
s[i] = '\0';
strcpy( buf[k], s);
s += (i+1);
k++;
n = n - i - 1;
i = 0;
}
}
printf("s:%s\n",s);
strcpy( buf[k], s );
for( j = 0; j<= k; j++ ){
printf("buf[%d]:[%s]\n",j,buf[j]);
}
printf("j:%d\n",j);
free(t);
return 0;
}
The main problem is that you are assigning s to t before allocating memory for s:
char *t = s;
and then you try to free t which point to an un-initialised memory.
If you compile your code with -Wall you will get a warning.
You should set t = s after the memory allocation of s.
Also, always check the return value of malloc (there is no need to cast it):
s = malloc(sizeof(char)*15);
if (s == NULL) {
// Error!
return 1
}

C string to array -uppercase character bug [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Given this string "red, blue, green" create an array that contains these colors as its elements. The code I've written below works but when I change the first letter of the colors to uppercase I get the output- Red, Blu\301-!Wree\316. How can I make this code more dynamic to work with words that start with uppercase as well? Thank you.
#include <stdio.h>
#include <stdlib.h>
int findLength(char string[]){
int l =0;
for(l = 0; string[l]!='\0'; l++){
}
return l;
};
char *stringToArray(char string[]){
int i = 0;
int j = 0;
char c = ',';
int n = 0;
int l = findLength(string);
char *str = (char *)malloc(l * sizeof(char));
while(string[i] != l){
if(string[i] == c || string[i] != '\0'){
for(n = j; n < i; n++){
str[j++] += string[n];
}
}
i++;
}
printf("%s\n", str);
str = '\0';
return str;
}
int main(int argc, const char * argv[]) {
char *string = "red, blue, green";
//char *string = "Red, Blue, Green";
char *str = stringToArray(string);
free(str);
return 0;
}
The strange behaviour doesn't have anything to do with whether your strings have upparcase letters or not. Your termination condition for the loop in stringToArray is wrong:
int l = findLength(string);
while (string[i] != l) ...
The condition should be
while (i < l) ...
or, as you have already used in findLength:
while (string[i] != '\0') ...
Because the condition is wrong – l is 16 in your case and none of the letters have an ASCII value of 16 – you go beyond the valid bounds of the string, which leads to undefined behaviour.
At the moment, you just copy the old string to the new one, albeit in a very strange fashion. Your inner loop makes use of three variables, of which it increments two. That's very confusing. It probably also doesn't do what you think, because the condition:
if (string[i] == c || string[i] != '\0') ..
is true for all letters of the string, provided that the opuer loop should consider only valid characters up to, but not including the end of the string.
Finally, if you want to copy the string, you should allocate süpace for the terminating character:
char *str = malloc(l + 1);
When you want to append the final null character:
str = '\0';
You actually set the while allocated string to null, which leads to a memory leak. (The free in main doesn't produce an error, because free can legally take ´NULL` as argument.) Instead, use:
str[l] = '\0';
With these fixes, you now have a program that copies the original string. The (POSIX) library function strdup does this more effectively. If you want to return an array of strings, you must reflect your function to return a pointer to pointers of chars.
Below is a possible implementation of that behaviour. (It uses the approach to allocate memory for everything on the heap. If you always expect three short strings that may not be the best solution.)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char **stringToArray(const char *str)
{
char **res;
const char *p = str;
int n = 0;
while (p) {
p = strchr(p, ',');
if (p) p++;
n++;
}
res = malloc((n + 1) * sizeof(*res));
p = str;
n = 0;
while (p) {
const char *begin;
size_t len;
while (*p == ' ') p++;
begin = p;
p = strchr(p, ',');
if (p) {
len = p - begin;
p++;
} else {
len = strlen(begin);
}
res[n] = malloc(len + 1);
memcpy(res[n], begin, len);
res[n][len] = '\0';
n++;
}
res[n] = NULL;
return res;
}
int main(int argc, const char * argv[])
{
char *str = "Vermilion, Ultramarine, Chartreuse";
char **res = stringToArray(str);
int i = 0;
for (i = 0; res[i]; i++) {
puts(res[i]);
free(res[i]);
}
free(res);
return 0;
}
you have a few mistakes...
I've corrected them for you:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int findLength(char string[]){
int l = 1;
for (int i = 0; string[i] != '\0'; i++){
if (string[i] == ',')// to check the end of a color
l++;
}
return l;
};
char **stringToArray(char string[]){//added a * for array of satrings
int i = 0;
int j = 0;
char c = ',';
int n = 0;
int l = findLength(string);
char **str = (char **)malloc(l * sizeof(char*)+l);
char *pos = string;
for (int i = 0; i < l-1; i++) //getting each color to the array
{
char *c =strchr(string, ',');
int index = c - pos;
string[index] = 0;
str[i] = _strdup(pos); //copying the color to the array
pos = c + 1;
string = string +1 +index; // next color
}
str[l - 1] = _strdup(pos); //copying last color
for (int i = 0; i < l; i++) //printing the results
{
printf("%s\n",str[i]);
}
return str;
}
int main(int argc, const char * argv[]) {
char string[] = "red,blue,green"; //deleted spaces
char **str = stringToArray(string);
getchar();
free(str);
return 0;
}
also added comments for you to understand.

String tokenizer without using strtok()

I'm in the process of writing a string tokenizer without using strtok(). This is mainly for my own betterment and for a greater understanding of pointers. I think I almost have it, but I've been receiving the following errors:
myToc.c:25 warning: assignment makes integer from pointer without a cast
myToc.c:35 (same as above)
myToc.c:44 error: invalid type argument of 'unary *' (have 'int')
What I'm doing is looping through the string sent to the method, finding each delimiter, and replacing it with '\0.' The "ptr" array is supposed to have pointers to the separated substrings. This is what I have so far.
#include <string.h>
void myToc(char * str){
int spcCount = 0;
int ptrIndex = 0;
int n = strlen(str);
for(int i = 0; i < n; i++){
if(i != 0 && str[i] == ' ' && str[i-1] != ' '){
spcCount++;
}
}
//Pointer array; +1 for \0 character, +1 for one word more than number of spaces
int *ptr = (int *) calloc(spcCount+2, sizeof(char));
ptr[spcCount+1] = '\0';
//Used to differentiate separating spaces from unnecessary ones
char temp;
for(int j = 0; j < n; j++){
if(j == 0){
/*Line 25*/ ptr[ptrIndex] = &str[j];
temp = str[j];
ptrIndex++;
}
else{
if(str[j] == ' '){
temp = str[j];
str[j] = '\0';
}
else if(str[j] != ' ' && str[j] != '\0' && temp == ' '){
/*Line 35*/ ptr[ptrIndex] = &str[j];
temp = str[j];
ptrIndex++;
}
}
}
int k = 0;
while(ptr[k] != '\0'){
/*Line 44*/ printf("%s \n", *ptr[k]);
k++;
}
}
I can see where the errors are occurring but I'm not sure how to correct them. What should I do? Am I allocating memory correctly or is it just an issue with how I'm specifying the addresses?
You pointer array is wrong. It looks like you want:
char **ptr = calloc(spcCount+2, sizeof(char*));
Also, if I am reading your code correctly, there is no need for the null byte as this array is not a string.
In addition, you'll need to fix:
while(ptr[k] != '\0'){
/*Line 44*/ printf("%s \n", *ptr[k]);
k++;
}
The dereference is not required and if you remove the null ptr, this should work:
for ( k = 0; k < ptrIndex; k++ ){
/*Line 44*/ printf("%s \n", ptr[k]);
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void myToc(char * str){
int spcCount = 0;
int ptrIndex = 0;
int n = strlen(str);
for(int i = 0; i < n; i++){
if(i != 0 && str[i] == ' ' && str[i-1] != ' '){
spcCount++;
}
}
char **ptr = calloc(spcCount+2, sizeof(char*));
//ptr[spcCount+1] = '\0';//0 initialized by calloc
char temp = ' ';//can simplify the code
for(int j = 0; j < n; j++){
if(str[j] == ' '){
temp = str[j];
str[j] = '\0';
} else if(str[j] != '\0' && temp == ' '){//can omit `str[j] != ' ' &&`
ptr[ptrIndex++] = &str[j];
temp = str[j];
}
}
int k = 0;
while(ptr[k] != NULL){//better use NULL
printf("%s \n", ptr[k++]);
}
free(ptr);
}
int main(){
char test1[] = "a b c";
myToc(test1);
char test2[] = "hello world";
myToc(test2);
return 0;
}
Update: I tried this at http://www.compileonline.com/compile_c99_online.php
with the fixes for lines 25, 35, and 44, and with a main function that called
myToc() twice. I initially encountered segfaults when trying to write null characters
to str[], but that was only because the strings I was passing were (apparently
non-modifiable) literals. The code below worked as desired when I allocated a text buffer and wrote the strings there before passing them in. This version also could be modified to return the array of pointers, which then would point to the tokens.
(The code below also works even when the string parameter is non-modifiable, as long as
myToc() makes a local copy of the string; but that would not have the desired effect if the purpose of the function is to return the list of tokens rather than just print them.)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void myToc(char * str){
int spcCount = 0;
int ptrIndex = 0;
int n = strlen(str);
for(int i = 0; i < n; i++){
if(i != 0 && str[i] == ' ' && str[i-1] != ' '){
spcCount++;
}
}
//Pointer array; +1 for one word more than number of spaces
char** ptr = (char**) calloc(spcCount+2, sizeof(char*));
//Used to differentiate separating spaces from unnecessary ones
char temp;
for(int j = 0; j < n; j++){
if(j == 0){
ptr[ptrIndex] = &str[j];
temp = str[j];
ptrIndex++;
}
else{
if(str[j] == ' '){
temp = str[j];
str[j] = '\0';
}
else if(str[j] != ' ' && str[j] != '\0' && temp == ' '){
ptr[ptrIndex] = &str[j];
temp = str[j];
ptrIndex++;
}
}
}
for (int k = 0; k < ptrIndex; ++k){
printf("%s \n", ptr[k]);
}
}
int main (int n, char** v)
{
char text[256];
strcpy(text, "a b c");
myToc(text);
printf("-----\n");
strcpy(text, "hello world");
myToc(text);
}
I would prefer simpler code, however. Basically you want a pointer to the first non-blank character in str[], then a pointer to each non-blank (other than the first) that is preceded by a blank. Your first loop almost gets this idea except it is looking for blanks preceded by non-blanks. (Also you could start that loop at i = 1 and avoid having to test i != 0 on each iteration.)
I might just allocate an array of char* of size sizeof(char*) * (n + 1)/2 to hold the pointers rather than looping over the string twice (that is, I'd omit the first loop, which is just to figure out the size of the array). In any case, if ptr[0] is non-blank I would write its address to the array; then looping for (int j = 1; j < n; ++j), write the address of str[j] to the array if str[j] is non-blank and str[j - 1] is blank--basically what you are doing, but with fewer ifs and fewer auxiliary variables.
Less code means less opportunity to introduce a bug, as long as the code is clean and makes sense.
Previous remarks:
int *ptr = declares an array of int. For an array of pointers to char, you want
char** ptr = (char**) calloc(spcCount+2, sizeof(char*));
The comment prior to that line also seems to indicate some confusion. There is no terminating null in your array of pointers, and you don't need to allocate space for one, so possibly spcCount+2 could be spcCount + 1.
This also is suspect:
while(ptr[k] != '\0')
It looks like it would work, given the way you used calloc (you do need spcCount+2 to make this work), but I would feel more secure writing something like this:
for (k = 0; k < ptrIndex; ++k)
I do not thing that is what caused the segfault, it just makes me a little uneasy to compare a pointer (ptr[k]) with \0 (which you would normally compare against a char).

How to reassign a string?

I am trying to write a program which merges a lines from stdin and print only those sentences which are longer than 80 characters. The first found line works well - the later ones, however, are empty. I think that I am doing something wrong with the line
current_sentence = malloc(sentence_len);.
How can I reassign a string correctly?
Code
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# define BUFFERSIZE 100
char* merge_string(char *text[], int n){
int i;
char *result = malloc(BUFFERSIZE * n);
for (i=0; i < n; i++){
strcat(result, text[i]);
}
return result;
}
int main(int argc, char *argv[]){
char buffer[BUFFERSIZE];
int i = 0;
char *text[BUFFERSIZE];
while(fgets(buffer, BUFFERSIZE, stdin) != NULL){
text[i] = strdup(buffer);
i++;
}
char *sentence = merge_string(text, i);
int sentence_len = strlen(sentence);
int j = 0;
int counter = 0;
char *current_sentence = malloc(sentence_len);
while (j < sentence_len){
current_sentence[counter] = sentence[j];
if (sentence[j] == '\n' && counter >= 80){
printf(":::HIT:::%s\n\n\n", current_sentence);
counter = 0;
current_sentence = malloc(sentence_len);
}
else if (sentence[j] == '\n'){
puts("Resetting counter");
counter = 0;
}
j++; counter++;
}
return 0;
}
Output
make 1_17; ./1_17 < example.txt
make: `1_17' is up to date.
Resetting counter
Resetting counter
:::HIT:::SHenri Cartier-Bresson (1908-2004) said "Your first 10,000 photographs are your worst," but he shot more than one an hour.)
Resetting counter
:::HIT:::
Resetting counter
:::HIT:::
You are not terminating current_sentence with a null character ('\0'). If you want printf to print the string properly, better make sure it is null-terminated.
By the way, there's no need for a second malloc. Reuse the memory allocated for current_sentence without re-allocating.
Also note that you're not freeing the allocated memory properly. You should be use a matching free call for each malloc. Perhaps this isn't a problem now, but it creates a memory leak.
Your loop should look something like this:
while (j < sentence_len)
{
current_sentence[counter] = sentence[j];
if (sentence[j] == '\n')
{
if (counter >= 80)
{
current_sentence[counter + 1] = '\0'; // Make string null-terminated
printf(":::HIT:::%s\n\n\n", current_sentence);
}
else
{
puts("Resetting counter");
}
counter = 0;
}
else
{
counter++;
}
j++;
}
free(current_sentence); // Free allocated memory
Then again, as mentioned in a comment, you'd rather let fgets do the work for you indeed.
char *text[BUFFERSIZE];
should be
char text[BUFFERSIZE];

Resources