String manipulation code using char ** pointer gives unexpected result

142 views Asked by At

I was trying to implement a string parsing code for that I need substring of the given string so I deed the following:

The header file is : test.h

#ifndef header_file
#define header_file

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

int increment(char **);

#endif  

The source files : main.c test.c

test.c :

  1. Case1 :test.c

    #include"test.h"

    int increment(char **string){

        char *temp = *(string);
        int value;
    
    
        if(temp != NULL){
    
                *(string) = ++temp;
                value = 1;
        }
    
        else{
                value = 0;
        }
    
        return value;
    

    }

  2. Case2 :test.c

    #include"test.h"

    int increment(char **string){

        char *temp = *(string);
        int value;
    
    
        if(*temp != '\0'){
    
                *(string) = ++temp;
                value = 1;
        }
    
        else{
                value = 0;
        }
    
        return value;
    

    }

main.c:

#include"test.h"

int main()
{
        char str[30] = "I have done form here comes.";
        char strs[50];

        char *p = str;

        memset(strs, 0, 50);

        while(increment(&p))
        {
                strcpy(strs,p);
                printf("Originally the string is : %s\n", str);
                printf("The modified string is   : %s\n", strs);
                memset(strs, 0, 50);
        }

        return 0;
}

The makefile is :

#This is the makefile.

all : run main.o test.o

run : main.o test.o
        $(CC) -g $^ -o $@

%.o : %.c
        $(CC) -g -c $^ -o $@

.PHONY : clean

clean : 
        -rm -f *.o run

But in the first case in test.c where I tried to traversed the sub-string but It is giving some garbage result. And second case works fine.

What is going wrong in test.c case 1.

thanks!!!!!!!!!!!!

1

There are 1 answers

0
rr- On BEST ANSWER

The purpose was to add prefix and suffix to every word in the string so I need the string before the word , the word , and after the word. ex: prefix_I_suffix have done form here comes. I prefix_have_suffix have done form here comes.

You're doing a few things wrong.

  1. I don't see splitting words by spaces anywhere at all, and that seems to be your main requirement.
  2. I see, however, some insane operations on pointers. Frankly, I can't understand the logic behind them.

So let me give you examples how it can be done - without modifying any pointers at all:

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

int main ()
{
    char str[] = "I have done answer here comes.";

    const char *delimiters = " ";
    char *pch = strtok(str, delimiters);
    while (pch != NULL)
    {
        printf("prefix_%s_suffix\n", pch);
        pch = strtok(NULL, delimiters);
    }
    return 0;
}

With copying the input to intermediate array that you can modify however you want:

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

int main ()
{
    char str[] = "I have done answer here comes.";

    char **array = NULL;
    size_t array_size = 0;

    const char *delimiters = " ";
    char *pch = strtok(str, delimiters);
    while (pch != NULL)
    {
        size_t str_size = strlen(pch) + 1; //to accommodate for `\0` byte
        char *str_copy = malloc(str_size);
        if (!str_copy)
        {
            printf("No memory!");
            return 1;
        }
        memcpy(str_copy, pch, str_size);

        ++ array_size;
        array = realloc(array, sizeof(char*) * array_size);
        if (!array)
        {
            printf("No memory!");
            return 1;
        }
        array[array_size - 1] = str_copy;

        pch = strtok(NULL, delimiters);
    }

    for (size_t i = 0; i < array_size; i ++)
    {
        printf("prefix_%s_suffix\n", array[i]);
        free(array[i]);
    }
    free(array);

    return 0;
}