- Joined
- Feb 19, 2022
- Messages
- 3
- Reaction score
- 0
im having trouble with pointers. This isnt returning what Im expecting after the first iteration. Im about 80% confident the problem is inside my if statement. I think Im using the pointers incorrectly.
/******************************************************************************
Function: find_byte
Description: Given two pointers(from and to), scan the range looking for the
first occurrence of byte, returning a pointer to it. If byte is
not found, return NULL. The pointers are guaranteed to be pointing
into the same array and that the address of from is less than or
equal to the address of to.
Input(s):
Outputs:
******************************************************************************/
const char *find_byte(const char *from, const char *to, char byte)
{
int i, k; /*loop variables*/
char *bytes = &byte;
/*start at *from*/
for(i = 0; from + i <= to; i++)
{
/*start at beggining of byte string*/
for(k = 0; *(bytes + k) != 0; k++)
{
/*check if first letter of byte = *from*/
if(*(from + i) == *(bytes + k))
{
/*true = return *byte*/
return from + i;
}
else
{
/*false = return NULL*/
return NULL;
}
/*repeat on next letter of byte string*/
}
/*at end of byte string move to next letter of main string(*from + 1)*/
/*repeat until from = to*/
}
return NULL;
}
/******************************************************************************
Function: find_byte
Description: Given two pointers(from and to), scan the range looking for the
first occurrence of byte, returning a pointer to it. If byte is
not found, return NULL. The pointers are guaranteed to be pointing
into the same array and that the address of from is less than or
equal to the address of to.
Input(s):
Outputs:
******************************************************************************/
const char *find_byte(const char *from, const char *to, char byte)
{
int i, k; /*loop variables*/
char *bytes = &byte;
/*start at *from*/
for(i = 0; from + i <= to; i++)
{
/*start at beggining of byte string*/
for(k = 0; *(bytes + k) != 0; k++)
{
/*check if first letter of byte = *from*/
if(*(from + i) == *(bytes + k))
{
/*true = return *byte*/
return from + i;
}
else
{
/*false = return NULL*/
return NULL;
}
/*repeat on next letter of byte string*/
}
/*at end of byte string move to next letter of main string(*from + 1)*/
/*repeat until from = to*/
}
return NULL;
}