Jump to content

C pointer, begin search from address

ShatteredPsycho

Hello everyone,

So I am doing some exercices about C programming, specifically an introduction to pointers. I have this one exercise:

Quote

Implement a function char* find_word(char* str, char* word, char* initial_addr) to search for a word in a string and returns the address in which it was found, or NULL otherwise. The function must find words whether in upper or lower case (consider that the string has only letters and spaces). The function receives in initial_addr the address within the string from which the search should be initiated.

And I am not sure how to go about the initial_address. Should I do something like:

char str[]={"This is a string"};
char *index = *str[2];

And then begin to search from index? How would I do that? How do I say that an array starts on the position pointed by the index?

Link to comment
Share on other sites

Link to post
Share on other sites

The thing with arrays is you can grab the address of one of the elements in the array and put it into a pointer. If you dereference the pointer, it'll return what was in that element.  So for example:

char str[] = {"This is a string."};
char* start = (char*)str; // This will point to the beginning of the array
char* mid_start = &str[6]; // This will point to element 6 in the array
char* end = &str[sizeof(str)-1]; // This will point to the end of the array, which is a NUL character

printf("%c\n", *start);			//Prints T
printf("%c\n", *mid_start);		//Prints s
printf("%c\n", *end);			//Prints nothing

The fun thing is you can add offsets to pointers, since they're addresses. Since an array is a block of memory with each element only being on part of that block, this magic happens:

char str[] = {"This is a string."};
char* start = (char*)str; // This will point to the beginning of the array

printf("%c\n", *start);			//Prints T
printf("%c\n", *(start+6));		//Prints s
printf("%c\n", *(start+(sizeof(str)-1)));	//Prints nothing

So what they mean by initial_address is it wants some place in the string to start. It can be the address where str[0] lives or somewhere else. So if you wanted to start the search in the middle of the string, at say element 6, you'd write the call like:

// Assume str and word are defined
find_word(str, word, &str[6]); //Start at the 6th index of str
find_word(str, word, (char*)str + 6); //Also start at the 6th index of str

 

Important: If you use an address that's out of bounds of the block of memory where the array lives, you'll run into problems. If you're running this code in an environment running virtual memory and various levels of memory protection, you'll likely get a segmentation fault (i.e., trying to access memory that isn't yours) and the program exits. But if you're running this on a system with no memory protection, the code will happily let you do whatever it is you wanted to do beyond the bounds of the array. So make sure you police yourself when it comes to array accessing in C by either guaranteeing the array won't be accessed beyond its bounds or by checking where you're trying to access before calling find_word.

Edited by M.Yurizaki
Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, M.Yurizaki said:

The thing with arrays is you can grab the address of one of the elements in the array and put it into a pointer. If you dereference the pointer, it'll return what was in that element.  So for example:


char str[] = {"This is a string."};
char* start = (char*)str; // This will point to the beginning of the array
char* mid_start = &str[6]; // This will point to element 6 in the array
char* end = &str[sizeof(str)-1]; // This will point to the end of the array, which is a NUL character

printf("%c\n", *start);			//Prints T
printf("%c\n", *mid_start);		//Prints s
printf("%c\n", *end);			//Prints nothing

The fun thing is you can add offsets to pointers, since they're addresses. Since an array is a block of memory with each element only being on part of that block, this magic happens:


char str[] = {"This is a string."};
char* start = (char*)str; // This will point to the beginning of the array

printf("%c\n", *start);			//Prints T
printf("%c\n", *start+6);		//Prints s
printf("%c\n", *start+(sizeof(str)-1));	//Prints nothing

So what they mean by initial_address is it wants some place in the string to start. It can be the address where str[0] lives or somewhere else. So if you wanted to start the search in the middle of the string, at say element 6, you'd write the call like:


// Assume str and word are defined
find_word(str, word, &str[6]); //Start at the 6th index of str
find_word(str, word, (char*)str + 6); //Also start at the 6th index of str

 

Important: If you use an address that's out of bounds of the block of memory where the array lives, you'll run into problems. If you're running this code in an environment running virtual memory and various levels of memory protection, you'll likely get a segmentation fault (i.e., trying to access memory that isn't yours) and the program exits. But if you're running this on a system with no memory protection, the code will happily let you do whatever it is you wanted to do beyond the bounds of the array. So make sure you police yourself when it comes to array accessing in C by either guaranteeing the array won't be accessed beyond its bounds or by checking where you're trying to access before calling find_word.

Thanks for the incredible response!

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, ShatteredPsycho said:

Hello everyone,

So I am doing some exercices about C programming, specifically an introduction to pointers. I have this one exercise:

And I am not sure how to go about the initial_address. Should I do something like:


char str[]={"This is a string"};
char *index = *str[2];

And then begin to search from index? How would I do that? How do I say that an array starts on the position pointed by the index?

It's a bit weird. Parameter "str" would be superfluous. All you'd need is the "initial_addr" as the starting point for the search and "word", the word to search. Either it's just weird or there's some hidden catch to the exercise (?)

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, Unimportant said:

It's a bit weird. Parameter "str" would be superfluous. All you'd need is the "initial_addr" as the starting point for the search and "word", the word to search. Either it's just weird or there's some hidden catch to the exercise (?)

Yeh figured the same after the answer @M.Yurizaki gave and talking to him.

I am just going to pick the initial_address and iterate over it

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×