Help with pointers


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

Advertisements

Joined
Mar 3, 2021
Messages
243
Reaction score
29
It's honestly a bit hard to tell what you're doing in that loop. But, there is a definite problem with char *bytes = &byte;. You're getting the memory location of a stack variable and comparing stuff to that. Besides that, this is very over complicated. One hint, if you will, is that the characters in the const char* variables are constant/immutable, not the pointers themselves. I'd recommend comparing *from to byte and moving forward if they don't match. Stop comparing when from is higher than to.

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

/******************************************************************************
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){
        while (from <= to){
                if (*from == byte){
                        return from;
                }
                from++;
        }
        return NULL;
}
void main(){
        const char* input = "abcdefghijklmno";
        printf("%s\n", find_byte(input, input + strlen(input), 'g'));
        printf("done");

        return;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top