returning char array

I want to return a char array to the main() function, but its returning garbage value.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include<stdio.h>
//#include<conio.h>
#include<string.h>

char* strtrmm();

int main()
{

char str1[100],c1[100];
printf("\n Enter the string:");
 gets(str1);

//strtrmm(str1);
printf("%s",strtrmm(str1));     //gives garbage value

//printf("%s",c1);
}

char* strtrmm( char str1[])
{
 char str[100],sub[100],s1[100],s11[100],*c1="a";
 int i,i1=0,j,k,l,f=0,g=0,m,h,v,count=0;
 
 

	
	strcpy(str,str1);

 printf("\n Enter the substring:");
 gets(sub);

 l=strlen(sub);
 m=strlen(str);

 printf("%d %d",l,m);
 for(i=0;str[i]!='\0';i++)
 {
  k=i;
  j=0;
  while(sub[j]!='\0')
  {
   if(str[k]==sub[j])
   {
    g++;
    if(g==l)
    {
     f=1;
     h=i;
    }
   }
   else if(str[k]!=sub[j])
   {
    break;
   }
   j++;
   k++;
  }
 }
 if(f==1)
 {
  printf("\n sub string is found\n\n\n");
  for(i=0;i<m;i++)
  {
   if(i<h)
	{
    printf("%c",str[i]);
	s1[i1]=str[i];
	i1++;
	count++;
	}	
   else if(i>h&&i<(h+l))
    continue;
   else if(i>=(h+l))
	{
    printf("%c",str[i]);
	s1[i1]=str[i];
	i1++;
	count++;
	}
  }
printf("\n %d \n",count);
 }
 else if(f==0)
 {
  printf("\n substring not found:");
 }

/*	for(i=0;i<count;i++)
	{
	s11[i]=s1[i];

	}
*/

c1=s1;


printf(" ret:%s \n ",c1);
return c1;
// getch();
 //return(0);
}


Basically, you are returning 's1'. However, 's1' is declared on the stack, and locally, so it has automatic storage duration. This means that when the scope finishes, it loses scope and is cleaned off the stack, leaving garbage in its place.

What you need to do instead is either pass into the function an array to modify, or to declare the array s1 on the heap (using malloc). If you go for the second choice, make sure you free it after you are done with it!
I would say, it's returning garbage value because it contains garbage code.
However:
it doesn't compile, you have to change line 5 to char* strtrmm( char str1[]);.
The better (and simplest) way is to use a std::string.
Some hints:
1
2
#include <string>
#include <iostream> 

char* strtrmm( char str1[]) will be std::string strtrmm( char str1[])
and printf("%s",strtrmm(str1)); will be std::cout << strtrmm(str1);
and you have to initialize variables, example: s1[100]={0}.
So it will work, but it will remain garbage code.
@fcantoro
Please don't go on holy language wars. OP is using C.

@zinat
Your strtrmm() function should not be asking the user for input. That should be happening in main().

Lines 5 and 20 don't match (but they should).
I always recommend putting main() last, but everyone seems dead-set on zillions of prototypes at the top of the file to track what follows in the file... so do what your instructor asks.

For a homework assignment, I can't give you grief, but in the future, don't use gets(). It's an evil function and should never have existed. (That's not just my opinion, BTW, but the opinion of experts. The function is dangerous.)

In any case, your strtrmm() function needs to know:
- the string to be searched
- the string to find
- (optionally) the index to begin searching in (the string to be searched)

I am unsure, looking at your code, what you are trying to return. It should be one of the following:
- the address in (the string to be searched) of (the string to find)
- the index into (the string to be searched) of (the string to find)
- the number of times in (the string to be searched) that (the string to find) is found

If you opt to return the address of the find, remember that the address is somewhere inside the existing string (array), so your function can look like this:

char* strtrmm( char* searchme, char* findme );

Remember that the result will be somewhere inside 'searchme', so if you were to call the function like this:

1
2
3
char* s = "Hello world";

char* t = strtrmm( s, s );

Then the result would be (t == s).

Likewise, for:

1
2
3
char* s = "Hello world";

char* t = strtrmm( s, "world" );

the result would be (t == (s+6)).

In any case, you shouldn't be printing straight from the result, since you should not modify the 'searchme' string. For example:

1
2
// this:
printf( "%s\n", strtrmm( "Hello world", "ello" );   
// produces:
ello world

It found "ello", but the string ends after the 'd' in "world". You would need to copy the substring out -- BUT, it is unnecessary anyway, as you already have a copy of the substring.

1
2
3
4
5
6
7
8
9
char* a = "Hello world";
char* b = "ello";

char* t = strtrmm( a, b );

if (t != NULL)
  printf( "Found %s\n", b );
else
  puts( "Not found!" );

Notice also that when 'findme' is not found, the function should return something useful, like NULL or the address of the null at the end of the string.


Your actual assignment is unclear, so I hope these ruminations help.
Last edited on
@Duoas
Excuse me, but this is a C++ forum, so I thought the OP was using C++, so I proposed std::string and std::cout, that are simpler and clearer.
And, I never said anything about the language, my comments were regarding the coding style, like using variables named i,i1=0,j,k,l,f=0,g=0,m,h,v and the lack of comments.
My "garbage code" definition was a spontaneous gag (if we could say in English), result of its definition of "garbage value", I didn' want to offend, I know the OP is a beginner.
However, I don't want to start a flame here.
fcantoro wrote:
I don't want to start a flame here.
Odd then how you are taking some effort to do just that.

C is a subset of C++, and it is not unnatural to get help with C on a C++ forum. Further, there is no place where this forum even hints that C questions are unwelcome. Hence... we shouldn't be purists here.

An express comment about coding style, as you just gave, is clear and succinct. Calling someone's code "garbage" is not.

My comments were clear: Implied: you are going on a holy language war (whether you are aware of it or not). Explicit: please don't. Explicit reason: OP is using C, not C++. It is not an attack.

I've personally learned that "spontaneous gags" will always be confusing to at least some people, and taken the wrong way. I tried to google up a thread where I made a dumb joke and it went all wrong, but I can't find it. Something about misunderstanding "bro".

So your (qualified) apology is accepted, if you accept mine (qualified) apology that I wasn't intending to offend you, only to point out that you missed the C part. (Maybe you were tired? Happens to me all the time.)
Topic archived. No new replies allowed.