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!!!!!!!!!!!!
(temp != NULL), which ultimately governs your main loop, will never be false, because the way thatincrementworks,pwill point to a char instr, which isn'tNULL. Instead, you should check that the value pointed at isn't the null character:(*temp != '\0'). - M Oehm*.m4a? This makes no sense. Your program doesn't contain such text at all. And even if it was instr[30], I don't see code responsible for splitting words anywhere. - rr-