Returning char array


 
Thread Tools Search this Thread
Top Forums Programming Returning char array
# 1  
Old 05-15-2014
Returning char array

I want to return a char array to the main() function, but its returning garbage value.
Code:
#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);
}

# 2  
Old 05-15-2014
At the very least, you should provide a description of what the program does, along with sample input, current (incorrect) output, and the desired output.

It would also help a great deal if you used a reasonable amount of indentation to highlight your code's structure.

Regards,
Alister
# 3  
Old 05-15-2014
s1 is a local variable. It ceases to exist when your function returns.

Usually, such a function would accept str1 as an argument instead of declaring it locally -- that would let you pass valid memory INTO the function. It would remain valid after strtrmm returns.

Code:
char *strtrmm(const char *in, char *str1);

int main(void) {
        char in[100]="some string";
        char out[100];

        printf("%s\n", strtrmm(in, out));
}

This would effectively be the same as
Code:
strtrmm(in, out);
printf("%s\n", out);

You should end your printf with \n if you want to actually see it, since without it you'll get all your strings jammed on one line and not even see them until the program quits.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How do I test the first char of each line in an array

Hi folks, I am self-learning as I can I have a script that has read a file into an array. I can read out each line in the array with the code: for INDEX in {0..$LENGTH} ## $LENGTH was determined at the read in do echo "${data}" done What I need to do is test the first char... (2 Replies)
Discussion started by: Marc G
2 Replies

2. Programming

char array

cat int.c int main() { unsigned char wwn; wwn=50; wwn=00; wwn=53; wwn=30; wwn=08; wwn=09; wwn=82; wwn=17; printf("WWN: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n ", wwn, wwn, wwn, wwn, wwn,... (8 Replies)
Discussion started by: powyama
8 Replies

3. Programming

help with char pointer array in C

i have an array like #define NUM 8 .... new_socket_fd = accept(socket_fd, (struct sockaddr *) &cli_addr, &client_length); char *items = {"one", "two", "three", "four", "five", "six", "seven", "eight"}; char *item_name_length = {"3", "3", "5", "4", "4", "3", "5", "5"}; ... (1 Reply)
Discussion started by: omega666
1 Replies

4. Programming

Array of char

I'm doing some coding in C++ Want to have a long empty string like below const char ModMisfit :: DelStr = "\r \r"; However due to the long blank the line is very long. Is there any way to avoid this and keep the... (5 Replies)
Discussion started by: kristinu
5 Replies

5. UNIX for Dummies Questions & Answers

Loop on array variable returning error: 0 not found

I'm guessing i have a syntax error. I'm not sure it get's past the the while condition. I get an error 0 not found. Simple loop not sure what I'm doing wrong. #!/usr/bin/ksh set -A MtPtArray /u03 /u06 tUbound=${#MtPtArray } echo $tUbound i=0 while ($i -lt $tUbound) do print... (4 Replies)
Discussion started by: KME
4 Replies

6. Programming

random array index returning values not contained

For kicks I wrote up a Password generator after lunch. Let me start with the code: unsigned int x,y,z,c; unsigned int KISS(); unsigned int devrand(); int main( int argc, char** argv ) { int i, j = 1; char pwd = "abcdefghijklmnopqrstuvwxyz" ... (5 Replies)
Discussion started by: VRoemer
5 Replies

7. Programming

Passing by value a char array

Hi I am passing or want to pass value of a char array, so that even thoug the called routine is changing the values the calling function should not see the values changed, meaning only copy should be passed Here is the program #include<iostream.h> #include<string.h> void f(char a); int... (5 Replies)
Discussion started by: rkraj
5 Replies

8. Programming

size of char array in c

i have to store a data more than 100000. i don't know the size of the data whether it may be 100000 or 1000000. so how can i define variable size; example char abc; but i don't know the size so how can i give array size?? in one sentence how can i give the array size dynamically so that i... (6 Replies)
Discussion started by: phani_sree
6 Replies

9. Programming

char array

hi, I have variable like, char keyword = "TRANSPARENCY "; while passing this variable to some function, first character of variable becomes null, but rest of characters still exist. Why this happens or something wrong with declaration. Their is no error while compiling & running... (2 Replies)
Discussion started by: avadhani
2 Replies

10. Programming

char array problem

hello i have a program in C (Unix - SOlaris5.7), and i have the next question: i have a lot of char variable, and i want store their values in a char array. The problem is what i donīt know how to put the char variable's value into the array, and i don`t know how to define the array please... (4 Replies)
Discussion started by: DebianJ
4 Replies
Login or Register to Ask a Question