string toupper

Hi, my question involves the logic behind a solution that i managed to come up with and often I feel like I code and magically solve the problems without a full understanding of how its working behind the scenes.

specifically when converting lowercase strings to uppercase. In my code:

for (int i = 0; arr[i] != '\0'; i++)
{


arr[i] = toupper(arr[i]);

}
//this is inside a function.

I have figured out the solution, but i feel like i just tinkered until i got it. My understanding (if right) is that this function will enter a for loop and will iterate until it hits a null character within the c-string array. The part that's not sitting well is the statement in the body of the loop. the array at index i is set to the function toupper that uses as it's parameter the array of index i? and from there it will update the index number until the termination of the array. I was trying to look at this in terms of pointers as well and was thinking that possibly the array at index/subscript i is set to this toupper function call with a parameter that acts as a point to the address of the starting point of the array and then iterates to the next address and updates the value associated with the address of the current index.

we just started learning pointers and want to make sure I am understanding this correctly. Because initially i first had the statement written as

arr[i] = toupper(i); but the text just disappeared from the screen when called.

also as a side note and off topic from this question... when someone says "the buffer" what is meant by this in relation to an array? thank you in advance for the assistance.
An array consists of a chunk of consecutively storaged elements of the same data type. If you define an array, for example char arr[5], then we reserve a block of five consecutively adjacent chars. The label 'arr' is internal a constant pointer to the first element and points to arr[0]. Thus you could for referring this first element by '*arr'. At this manner, each referencing by index can be done with a pointer notation. For example, the 5th element you could access by *(arr+4) instead arr[4]. In both cases the compiler calculates the storage address of the 5th element by additing the address of 'arr' with 4.

At your example arr[i] = toupper(arr[i]):
This instruction performs three steps:
1) It will pass the character lying at storage addres a[i] to the function toupper(),
2) toupper returns a 'char' value,
3) this returned value will be written to storage address arr[i].

A buffer is often implemented as an array. This means, that it has a distinct size. If the buffer-array is full, then it could happen the dreaded buffer overflow, and adjacent memory parts could be overwritten, which is a attack vector of some systems.
Last edited on
Topic archived. No new replies allowed.