invalid conversion from 'const char*' to 'unsigned int' [-fpermissive]

how do i fix this?

1
2
3
4
5
6
7
8
9
10
//Tests if the C string str starts with the specified prefix, the C string prefix.
bool startsWith(const char* str, const char* prefix)
{
	//just have to check once if first letter of str[0] == prefex, if so then true else false
	unsigned int i = prefix;
	if (str[0] == prefix)
		return true;
//default return is false
return false;
}
unsigned int i = prefix;
Remove this line.

You've got bigger problems, though.

(str[0] == prefix) str[0] is a char. prefix is a pointer - a memory address. You are trying to compare a char and a memory address. This makes no sense.
One way:
1
2
3
4
5
6
7
//Tests if the C string str starts with the specified prefix, the C string prefix.
bool startsWith(const char* str, const char* prefix)
{
  const char *pos = strstr(str, prefix);

  return pos == str;
}
Thomas 1965 have no idea, what you did there.

Repeater, yes i know trying to get a way around that, this is my first assignment in over 6 years. (we didn't even go over pointers, but i do know a little about it, it's an address in memory right?)
The function strstr returns a pointer to the beginning of the substring prefix in str.
Then you compare this beginning with the start of str.
If they are equal the start of str is the start of pos.

BTW. It would be easier to std::string instead of this old C stuff.
yes. If memory were a huge array, a pointer is the index into it.

strstr searches one string to see if it is inside another, eg abcd, ab, yes, ab is in abcd . If not inside it returns 0 (null pointer in C, these are C routines). If it is inside, it hands you the location of 'a' in my example, the starting point of the first place it found the substring you seek.

another way to do it is memcmp, and compare the first N bytes against a constant or a variable with the prefix, etc.

if you want to use C-strings, you really, really need to understand pointers and arrays well. That is what they ARE.
Last edited on
bool startsWith(const char* str, const char* prefix)
{
//
char pre = *prefix;
if (str == pre)
return true;
//default return is false
return false;
}

//but the problem is, i cannot make pre into an array (pre[15] to hold words), it only holds one letter and not the entire prefix.
we aren't allowed to do memcom functions or strstr functions yet unfortunately.
Used for loops in previous functions
1
2
3
4
5
6
7
8
9
10
11
bool startsWith(const char* str, const char* prefix)
{
  while ((*prefix != 0)  &&  (*str != 0) )
  {
     if (*prefix++ != *str++)
    {
       return false;
     }
  }
  return true;
}


This code has a bug; it will say that cat begins with catOnToast. Maybe you can fix that bug.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
ok, goodness.  I think this covers everything except null pointers passed
 into it.   Hopefully this is all correct; ppl been confusing me makin me lern
 c++ strings :P

bool startsWith(const char* str, const char* prefix)
{      
    int px = 0;
    int sx = 0;
    while(str[sx++]); //my teacher is not letting me code version of strlen
    while(prefix[px++]);
    if(px > sx) return false;  
     px = 0;
    while(prefix[px]) //my teacher is not letting me code version of memcmp
     {
         if(prefix[px] != str[px])
           return false;
          px++;
     }
      return true;
}


I don't usually like to do too much homework for people but I make an exception if they won't LET you do stuff. You should be able to do anything you can learn and get compiled and running.
Last edited on
Topic archived. No new replies allowed.