0

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

3
  • 2
    Well.. it's pretty clearly said - s1 is pointer, not a number(index). You can use additional counter, to count the index/position, or use pointer arithmetic to calculate the position. Commented May 21, 2015 at 13:34
  • @user3208656 And what to return if the character is not found in a string? Commented May 21, 2015 at 13:35
  • What is the intent of this function? Are you trying to return the index of the last '-' within s1? If so then you probably want this instead: int ft_replace(char const *s1) { int result; result = 0; for (const char* s = s1; *s != '\0'; ++s) { if (*s == '-') result = (s1-s); } return (result); } Commented May 21, 2015 at 13:39

4 Answers 4

3

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);
}
Sign up to request clarification or add additional context in comments.

Comments

1

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.

8 Comments

@Eregrith: You certainly can assign a char to an int, it just won't do anything useful in this case.
@Eregrith what about §6.5.16.1, paragraph 2?
@KirilKirov well, OP has logical issue in his code, that I have pointed out later. Otherwise, as per the question title, this is the way to remove the error. Now whether if fits the logic, that is another aspect (which also I addressed, I think).
@BillLynch And NatashaDutta you are both right. My bad
@Natasha Dutta How do you do?:)
|
0

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;
}

Comments

0

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);
}

Comments

Your Answer

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