C series for 1st sem students: Pointers in strings and scanf

Posted on Posted in Blogs, Technology

To be able to reap the best benefits from pointers, you’ll have to have a deep understanding of them. This blog is going to be very conceptual and we might not be writing real-world value code here, but these program snippets will help you use pointers in a very productive and efficient manner in future. We’ll discuss two use cases of pointers that we generally overlook: strings and the scanf function.


Scanf function:

We have been using the scanf function right from the beginning, but we don’t give much attention to how it works. For this reason a lot of errors may occur while using scanf function. Let us look into the header file stdio.h and check the definition of the scanf function.

scanf-definition jinsoft guwahatiAs we see in the highlighted line and also on the tooltip, the function takes a character pointer as the first argument and the three dots after that means it can take multiple additional arguments. The actual implementation code is in another file and it is beyond our scope of discussion here. Lets just look into the parameters of the function.

The first parameter expects a stream of characters, also called a string and is passed into the function by passing the pointer to the first character of that string. This is one of the best uses of pointers, as we’ll see in the next section when we discuss strings. The second parameter (…) represents capability to take in multiple arguments. Here we generally pass the variable where we want to store user input. A typical use of scanf might look like this.

scanf("%d", &number);

Here the first argument is the string ‘%d‘ and the second argument is the address of the variable number. Remember we pass the address of the variable but not the variable itself, i.e. we pass a pointer instead of the variable value.

Lets look at this program. Here we declare two integer variables for user input and a third to hold the sum of these numbers. In the scanf statement we pass three arguments. The first is a string which lets the program know what data-type of input should be expected. The second and third arguments are the addresses of the two variables.

The same result can be achieved by declaring pointers to the variables and passing those pointers in the scanf function, as illustrated below.

The pointer ptr is passed in the scanf function instead of directly passing &b because they are the same thing.


Strings in C:

String is not a separate data-type in C. Its a one-dimensional array of characters, terminated by a null character( \0 ). The null character is important because all one-dimensional arrays of characters are not necessarily strings. Also with the use of the null character we can represent a string with a character pointer. We point to the location of the first character of the array, and the end of the string is realized when a null character is encountered. Lets look at some examples.

In this first example, we initialize an array of char type, name and then assign a pointer myName to it. Note we didn’t use & in line 5, i.e. not  &name. This is because the name of an array acts like a pointer to the first element in it. Thus name[0] will give the value of the first element whereas name will give the address of the first element (try it yourself). While assigning a pointer to a string, it reads from memory until it encounters a null character. Therefore we don’t have to specify the length of the string.

In line 7 we initialize another string yourName and specify a maximum length of 20. We then request user input and use scanf to get the user input and save it in the array yourName. Note that we don’t use &yourName in line 11, because yourName is itself a pointer and it specifies the address of the first element of the array. The rest of the code is self explanatory.

In this example we need to be careful of the user input. If the name the user enters is longer than 19 characters, there will be no space for the null character at the end of the string yourName. This will result in runtime error. The scanf function should be used very carefully. To drive this point home, lets see another example.

string pointer jinsoftHere we initialize a string myName in the conventional syntax and two arrays of char type: myNameLetters and greeting. In the array myNameletters we add a null character at the end whereas keep the array greeting as a regular character array. When we try to print greeting as a string we see that it prints hellosudipta, i.e. it prints characters from memory till it hits the null character at the end of the previous array in memory, which is myNameLetters. We can further prove this point by using the function strlen available in the header file string.h. This function counts the number of characters before a null character is encountered.

As you can see, to use strings and arrays properly you’ll need to have a proper understanding of pointers. When we pass strings and arrays in functions we often use pointers as arguments. This is just scratching the surface, we’ll need to have excellent understanding of pointers to become awesome programmers. But we’ll get there soon.

If you are still confused, please comment below and I’ll try to clear your doubts.

You can download the code { here }

If you liked this article please comment and show your support and interest so that I’ll be motivated to continue this effort. Like our facebook page if you haven’t already. And if you have any questions please comment. I’ll try to reply all.