main()
{
int i=257;
float a=3.14;
char *ii,*aa;
ii=(char*)%26amp;i; //what (char*) is doing here
aa=(char*)%26amp;a;
printf("address in ii=%u",ii);
printf("address in aa=%u",aa);
printf("vallue of i=%d",*ii);
printf("vallue of a=%f",*aa);
}
Explain the output of the c prog?
ii is a pointer to a character and the line
ii=(char*)%26amp;i;
is pointing that pointer to the address of the integer in i. i is probably a 4 byte quantitiy (but could be 2 bytes depending on the platform). the (char*) is converting the pointer to an integer %26amp;i (read as address of i) into a pointer to a character. This is called casting as you are converting from an integer pointer type to a character pointer type. This helps the compiler decide how to proceed and make your code 'more' readable. This type of conversion is one of the strong and weak points of c programming. On one hand you may have good reason to treat the integer as characters or bytes, but on the other, the code becomes confusing and it is easy to step on your foot as you might cast what you think is an int into a float pointer and things start to quickly unravel.
aa=(char*)%26amp;a;
is very similar except you are pointing the character pointer as to the address of the floating point number a.
The next several lines print out the various combinations.
printf("address in ii=%u",ii);
This will print the actual address ii points to. It will not have much meaning beyond a possible debug session when everything is behaving strangly because your pointers seem to be causing problems.
printf("address in aa=%u",aa);
This will print the actual address aa points to.
printf("vallue of i=%d",*ii);
This prints out the value at the memory location that ii points to. The * dereferences the pointer and returns the pointers location. This could be different on different platforms.
printf("vallue of a=%f",*aa);
This prints out the value at the memory location that aa points to.
Reply:Hi!
First this is the main() function. Each program in C is required to declare and implement a main() function. When this program runs, it will run whatever is in the main() function first, which is why it is necessary to have one.
The first line says "int i=257;". This is declaring a variable. In C, variables are places in the computers memory that can hold values. Here we are declaring a variable named "i" that has a type of int. "int" stands for integer, and means any whole number, from approximately -32765 to 32767. Then we assign the value of 257 to the variable i. Later, we can refer to i to see what value it has.
The next line declares another variable, a float named "a" with a value of 3.14. The type "float" means floating point number, a number that is rational and not a whole number. This is why we can assign the value 3.14 to a float but not to an int.
Then we declare two more variables. You can define multiple variables of the same type by separating them with commas, like is done on this line. These variables are special, however, as they have an asterisk (*) preceding them. This means they are pointers. Pointers are a complex topic, but basically they are variables that point to another variable. These variables "ii" and "aa" have the type char, which is a character (any alpha-number character, 0-9, a-z, A-Z, etc.) but since they are pointer they won't hold a value, they will just point to another variable that has a value.
Then it assigns a value to ii. It assigns the %26amp;i, this means the address of i. The %26amp; means "address of". Every variable in C programming has a value, but it also has an address - a place in memory where it can store it's value. The %26amp; sign just says "tell me the place this variable has in memory". So ii, being a pointer, is now assigned the address of i. So ii now points to i. This is the main function of a pointer. As I said, this can be very confusing at first. The (char *) part is called a typecast. The address of a variable (%26amp;i) is not any defined type, it is just a location in memory. Since ii was defined as a char * (character pointer), you must typecast it to tell the computer that you want the location to be the right type that ii can store.
The next line is the same as the previous, just with aa and a. So now aa points to a.
Now we have a series of printf() statements. printf stands for print formatted. This is used for displaying text and variable's values to the screen. printf first takes a string of text and formatting characters, then a list of variables to output. In the first printf, it has the string "address in ii=%u". This is the string it will print to the screen, however, %u is a formatting symbol. The % sign tells the computer that you want to show the value of a variable and the u means that the variable is an unsigned type (unsigned means positive number). The next part of the printf statement has ii, so the %u will be replaced by the value of ii. This will effectively print the value of ii, which is the location of i in memory. (Which we defined earlier)
The next printf statement is the exact same, just a different variable.
The last two printf statements print the value of i and a respectively, however you will notice different formatting symbols. %d means decimal (a whole number) and %f means float (a floating point number). The only other difference here is that instead of just giving the variable ii, the variable is preceded by an asterisk (*). Without it, "ii" would give the address of the variable ii is pointing to, since it is a pointer. The asterisk says, "don't tell me the address ii is pointing to, tell me the value AT that address". Since ii points to i, instead of showing the address of i, it will print the value of i.
I hope this explanation sheds some light on the program's output and how it works. The output will not be the same on every computer because the address of variables in memory changes based on many things. However, the value of i will be shown as 257 and the value of a will be 3.14.
Pointers are a complex concept to grasp in C and many people struggle with it at first. If you would like some more tutorials to explain any of the concepts in C programming you can find some at www.cprogramming.com.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment