Well, I'm still studying pointers. At chapter 9 of Let Us C, there are a couple of programs that demonstrate switching 2 names in an array. Say the following names are entered/stored in an array:
Akshay Parag Raman Srinivas Gopal Rajesh
once the programs are run, the result should be this:
Akshay Parag Srinivas Raman Gopal Rajesh
The third and fourth names are interchanged.
The first program in the book demonstrates switching the names using a normal character array, the second demonstrates the same using an Array Of Pointers To Strings.
The book declares the name within the program but I wrote them using the scanf() function so that I could enter the names that I wanted, I did this for both the programs. As I read on the book mentions that we need to use a malloc() function to scanf() input from the keyboard when using an array of pointers to strings.
But I was already getting input in both the cases without the malloc() function, I don't even know what malloc() does. So I removed the .cpp extension from my file and saved it as .c, you know just in case it has something to do with C++ treating scanf() differently. Nope, still works. All I needed was an ampersand sign before the array name in the scanf() function.
I'm probably missing something, but why knock it when it works eh? =]
Here is the program:
#include <stdio.h>
main()
{
char *name[6][10],*t;
int i;
puts("Enter 6 Names:");
for(i=0;i<6;i++)
{
scanf("%s",&name[i]);
}
for(i=0;i<6;i++)
{
printf("%s ",&name[i]);
}
t=name[2][0];
name[2][0]=name[3][0];
name[3][0]=t;
printf("\n");
for(i=0;i<6;i++)
{
printf("%s ",&name[i]);
}
}
Now on to learning malloc() for me.
_______________________________________________________________________
EDIT:
Okay, I was wrong. Just as I suspected. The whole point (pun-ha) of using pointers to arrays of strings is to save memory space. Since I already declared that there would be 6 strings each upto 10 characters long
char *name[6][10]
It is pointless (pun again-ha) to use pointers to arrays if I'm going to use the same amount of space no matter what length of strings I enter. Now if I do not declare the length of the strings, the statement only seems to allocate 4 bytes of memory for each string, so that's 3 characters and one null character. Allow me to demonstrate:
#include <stdio.h>
main()
{
char *items[6];
int i;
puts("Enter the name of the items: ");
for(i=0;i<6;i++)
{
scanf(" %s",&items[i]);
}
for(i=0;i<6;i++)
{
printf("%s ",&items[i]);
}
printf("\n");
for(i=0;i<6;i++)
{
printf("%i ",&items[i]);
}
}
Akshay Parag Raman Srinivas Gopal Rajesh
once the programs are run, the result should be this:
Akshay Parag Srinivas Raman Gopal Rajesh
The third and fourth names are interchanged.
The first program in the book demonstrates switching the names using a normal character array, the second demonstrates the same using an Array Of Pointers To Strings.
The book declares the name within the program but I wrote them using the scanf() function so that I could enter the names that I wanted, I did this for both the programs. As I read on the book mentions that we need to use a malloc() function to scanf() input from the keyboard when using an array of pointers to strings.
But I was already getting input in both the cases without the malloc() function, I don't even know what malloc() does. So I removed the .cpp extension from my file and saved it as .c, you know just in case it has something to do with C++ treating scanf() differently. Nope, still works. All I needed was an ampersand sign before the array name in the scanf() function.
I'm probably missing something, but why knock it when it works eh? =]
Here is the program:
#include <stdio.h>
main()
{
char *name[6][10],*t;
int i;
puts("Enter 6 Names:");
for(i=0;i<6;i++)
{
scanf("%s",&name[i]);
}
for(i=0;i<6;i++)
{
printf("%s ",&name[i]);
}
t=name[2][0];
name[2][0]=name[3][0];
name[3][0]=t;
printf("\n");
for(i=0;i<6;i++)
{
printf("%s ",&name[i]);
}
}
Now on to learning malloc() for me.
_______________________________________________________________________
EDIT:
Okay, I was wrong. Just as I suspected. The whole point (pun-ha) of using pointers to arrays of strings is to save memory space. Since I already declared that there would be 6 strings each upto 10 characters long
char *name[6][10]
It is pointless (pun again-ha) to use pointers to arrays if I'm going to use the same amount of space no matter what length of strings I enter. Now if I do not declare the length of the strings, the statement only seems to allocate 4 bytes of memory for each string, so that's 3 characters and one null character. Allow me to demonstrate:
#include <stdio.h>
main()
{
char *items[6];
int i;
puts("Enter the name of the items: ");
for(i=0;i<6;i++)
{
scanf(" %s",&items[i]);
}
for(i=0;i<6;i++)
{
printf("%s ",&items[i]);
}
printf("\n");
for(i=0;i<6;i++)
{
printf("%i ",&items[i]);
}
}
Sample Input/Output:
This is what happens when you input acceptable characters:
Notice that the memory blocks are 4 spaces apart, so it takes in the 3 characters I input and stores the '\0' (NULL) character which tells the program that it is the end of the string in the last space. All good.
But when you input more characters than 3:
There is no space for the NULL character, so it just goes on to the next block, and the next until it encounters the NULL character for fig. That is why the output stops at fig. Except for the last Peach which sees no null value and we see that weird Clubs ASCII character at the end.
So technically, the computer does not know how many characters you will stick in there, so it gives you 4 bytes, at least thats what I think is happening.
So we need to use malloc() (Memory Allocate?) as soon as the user inputs the string to count the length and allocate the memory length for it.
So technically, the computer does not know how many characters you will stick in there, so it gives you 4 bytes, at least thats what I think is happening.
So we need to use malloc() (Memory Allocate?) as soon as the user inputs the string to count the length and allocate the memory length for it.
LESSON: If the book says you can't do it, but you find that you can, find out why you can, because the book is right.
No comments:
Post a Comment