17

I want to return a character array from a function. Then I want to print it in main. how can I get the character array back in main function?

#include<stdio.h>
#include<string.h>
int main()
{
    int i=0,j=2;
    char s[]="String";
    char *test;

    test=substring(i,j,*s);   
    printf("%s",test);
    return 0;
}


char *substring(int i,int j,char *ch)
{
    int m,n,k=0; 
    char *ch1;
    ch1=(char*)malloc((j-i+1)*1);
    n=j-i+1;

    while(k<n)
    {   
        ch1[k]=ch[i];
        i++;k++;
    }   

    return (char *)ch1;
}

Please tell me what am I doing wrong?

6
  • do you get any errors?? Commented May 8, 2013 at 14:50
  • 3
    Your compiler should have told you that test=substring(i,j,*s); you're dereferencing s when you shouldn't. Commented May 8, 2013 at 14:51
  • i am getting an error message : conflicting types for ‘substring’ Commented May 8, 2013 at 14:52
  • 1
    Unrelated to errors, but I believe both your (char*) casts are unnecessary and multiplication by 1 is pointless - (j-i+1)*1 should be simplified to j-i+1. Commented May 8, 2013 at 14:54
  • In addition to the other comments, you need to add a null terminator to ch1 at the end of the loop in substring Commented May 8, 2013 at 14:55

3 Answers 3

10
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char *substring(int i,int j,char *ch)
{
    int n,k=0;
    char *ch1;
    ch1=(char*)malloc((j-i+1)*1);
    n=j-i+1;

    while(k<n)
    {
        ch1[k]=ch[i];
        i++;k++;
    }

    return (char *)ch1;
}

int main()
{
    int i=0,j=2;
    char s[]="String";
    char *test;

    test=substring(i,j,s);
    printf("%s",test);
    free(test); //free the test 
    return 0;
}

This will compile fine without any warning

  1. #include stdlib.h
  2. pass test=substring(i,j,s);
  3. remove m as it is unused
  4. either declare char substring(int i,int j,char *ch) or define it before main
Sign up to request clarification or add additional context in comments.

2 Comments

it will compile fine without any warning //1.>include stdlib.h //2.>pass test=substring(i,j,s); //3.>remove m as it is unused //4.>either declare char substring(int i,int j,char *ch) or define it before main
Is it only me who sees the memory leak on test? (after 2,5 years ;) )
5

Lazy notes in comments.

#include <stdio.h>
// for malloc
#include <stdlib.h>

// you need the prototype
char *substring(int i,int j,char *ch);


int main(void /* std compliance */)
{
  int i=0,j=2;
  char s[]="String";
  char *test;
  // s points to the first char, S
  // *s "is" the first char, S
  test=substring(i,j,s); // so s only is ok
  // if test == NULL, failed, give up
  printf("%s",test);
  free(test); // you should free it
  return 0;
}


char *substring(int i,int j,char *ch)
{
  int k=0;
  // avoid calc same things several time
  int n = j-i+1; 
  char *ch1;
  // you can omit casting - and sizeof(char) := 1
  ch1=malloc(n*sizeof(char));
  // if (!ch1) error...; return NULL;

  // any kind of check missing:
  // are i, j ok? 
  // is n > 0... ch[i] is "inside" the string?...
  while(k<n)
    {   
      ch1[k]=ch[i];
      i++;k++;
    }   

  return ch1;
}

Comments

4

Daniel is right: http://ideone.com/kgbo1C#view_edit_box

Change

test=substring(i,j,*s);

to

test=substring(i,j,s);  

Also, you need to forward declare substring:

char *substring(int i,int j,char *ch);

int main // ...

1 Comment

im getting right result for integer type array. But when im trying the same with a char array im getting the error only

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.