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 :
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;
}
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!!!!!!!!!!!!
You're doing a few things wrong.
So let me give you examples how it can be done - without modifying any pointers at all:
With copying the input to intermediate array that you can modify however you want: