0

Im trying to return a pointer to a string in my function and assign to another char array to print it out. But i get a conflicting type error for my function.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[])
{
    const char *s = "[[random text]]";

    const char *P1 = "[[";
    const char *P2 = "]]";
    const char *result = parseString(s,P1,P2);
    printf("%s",result);
} 

void const char* parseString(char* str,char* first, char* snd)
{
    char *target = NULL;
    char *start, *end;

    if ( start = strstr( str, first ) )
    {
        start += strlen( first );
        if ( end = strstr( start, snd ) )
        {
            target = ( char * )malloc( end - start + 1 );
            memcpy( target, start, end - start );
            target[end - start] = '\0';
        }
    }
    return target;
}
2
  • Please post the exact error message as text (not as a screenshot) into the question. This error message should include a line number. You may want to quote the line of code that this error message is referring to, since Stack Overflow does not display line numbers. Commented Oct 14, 2022 at 15:45
  • Note that your code would be easier to read (both for yourself and for other people) if you used consistent indentation. I have fixed the indentation for you. Commented Oct 14, 2022 at 15:50

1 Answer 1

1

Your code has the following problems:

  1. You should provide a prototype declaration of the function parseString before using it in main.

  2. If you want the function parseString to return a pointer to a string, then you should make the return type of the function char * instead of void const char*.

  3. If you declare the parameters of the function parseString to be of type char*, then you are not allowed to pass that function an argument of type const char *. However, since the function parseString does not modify the strings pointed to by its arguments, you can declare the parameters to be const char * instead of char *, which will solve your problem.

After fixing these issues, your could will look like this:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char* parseString( const char* str, const char* first, const char* snd);

int main(int argc, char *argv[])
{
    const char *s = "[[random text]]";

    const char *P1 = "[[";
    const char *P2 = "]]";
    const char *result = parseString(s,P1,P2);
    printf("%s",result);
} 

char* parseString( const char* str, const char* first, const char* snd)
{
    char *target = NULL;
    char *start, *end;

    if ( start = strstr( str, first ) )
    {
        start += strlen( first );
        if ( end = strstr( start, snd ) )
        {
            target = ( char * )malloc( end - start + 1 );
            memcpy( target, start, end - start );
            target[end - start] = '\0';
        }
    }
    return target;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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