user3208656
user3208656

Reputation:

C - Assignment makes integer from pointer without a cast

I'm getting this error:
Warning: assignment makes integer from pointer without a cast [enabled by default]
This is my source code:

int     ft_replace(char const *s1)
{
    int     result;

    result = 0;
    for (; *s1 != '\0'; ++s1)
    {
        if (*s1 == '-')
            result = s1; // Warning here
    }

    return (result);
}

So, I'm getting an error ( warning ) and my result of my function ft_replace is fxc...
It return me a result like 4227111 and I don't know why
I would like my function when it find the last char here this is ' - ' it return the position of the array.

So a string which contains " Hel-l-o " -> Return 6
A string which contains " He-llo " -> Return 3
A string which contains " Hell-o- " -> Return 7

Upvotes: 0

Views: 15638

Answers (4)

Eregrith
Eregrith

Reputation: 4366

result = s1

result is an int, integer
s1 is a char const*, a const pointer to char

You can't assign the value of a const char* to an integer (because they might not have the same size for example).

You can however, as hinted by the warning, cast the value to the correct type to force it to stop warning you:

result = (int)s1;
          ^ this casts the value of s1 to the int type

But this is not a good practice as it can induce undefined behavior. Reading your code, I don't think you are doing what you think you are doing anyway when you assign s1 to result. What you should be doing, is incrementing a counter and set result during the loop:

int     ft_replace(char const *s1)
{
  int result;
  int i;

  result = -1;
  i = 0;
  for (; *s1 != '\0'; ++s1)
  {
    i++;
    if (*s1 == '-')
      result = i;
  }

  return (result);
}

Upvotes: 3

Klas Lindbäck
Klas Lindbäck

Reputation: 33273

A pointer holds the memory address, not the position in the array.

Use an index variable to keep track of the position.

Note that the first element in an array in C has index 0, hence the +1 when assigning result:

int     ft_replace(char const *s1)
{
    int     result, i;

    result = 0; 
    for (i = 0; s1[i] != '\0'; ++i)
    {
        if (s1[i] == '-')
            result = i + 1;
    }

    return (result);
}

Upvotes: 0

Vlad from Moscow
Vlad from Moscow

Reputation: 310980

I think you mean the following

int ft_replace( const char *s )
{
    int    pos = -1;
    const char *p = s;

    for ( ; *p != '\0'; ++p )
    {
        if ( *p == '-' ) pos = p - s;
    }

    return pos;
}

As for your code then this statement

result = s1;

does not make sense. In the left side of the assignment there is an object of type int while in the right side of the assignment there is a pointer of type const char *

If you want to return pointer to the last occurence of character '-' then the function can look like

char * ft_replace( const char *s )
{
    char *pos = NULL;

    for ( ; *s != '\0'; ++s )
    {
        if ( *s == '-' ) pos = ( char * )s;
    }

    return pos;
}

Upvotes: 0

Natasha Dutta
Natasha Dutta

Reputation: 3272

In your code, result is int and s1 is char const *.

You can change it to

 result = *s1;

However, there is no way to know the position of the array currently. You need to have a separate counter to keep track of the valid element present in the array.

Upvotes: 1

Related Questions