Saturday, September 6, 2008

C...learning: 1st Day (4-9-08)

Well the day before-yesterday at 4th September, I've learned the basics of C by reading the 1st from the 8 chapters my 290pages book includes.

Constants, Variables, using of the "%" operator, basic of pointers, the printf, scanf functions.

Mostly i was familiar with the staff from my C++ programming experience, yet I found difficulties

The Nature of Pointers

I was confused because I couldn't declare the pointer wright to the Variable bu just to its refference

int x=5;int y=5;int* pointer=&y;

printf(" x is, %d\n",x);
printf(" y is, %d\n",y);
printf(" &y is, %d\n",&y);
printf(" pointer is, %d\n",pointer);
printf(" *pointer is, %d\n",*pointer);
printf(" &*pointer is, %d\n",&*pointer);

scanf("a");
return 0;

OUTPUT

x is, 5
y is, 5
&y is, 2293604
pointer is, 2293604
*pointer is, 5
&*pointer is, 2293604

But by asking on the forums, until yesterday I've cleared up things as I learned that a pointer its by word an adress and so can only be assigned to an adress.

   P.S. Was confused until I 've outputed the pointer itself "printf(" pointer is, %d\n",pointer);"

However the "*" symbol is nothing more than a guidline to force the pointer show the content of its adress

And the "&" symbol on the contrary forces the revealance of the adress of a variable.

The Fact that when you were outputing "&*pointer" you were getting the adress confused me.

"Why do that?! The pointer is an adress itself!"

But what I've understood quickly was that when you had "*pointer" since the "*" forces teh content revealance, it is like having the variable itself (at least thats the way i saw it), and so, when you put a "&" in front of "*pointer" its like putting it in front of the variable, so you get the adress.

I've created this image to have a schematical reprsentation in my mind

Well I believe it was good I've cleared absolutely this basic fact in the initials of pointers. Empty spaces like this can be dreadfull later. I've experienced that with classes on C++

Well before I proceed to the next chapter I want to clear up things with the "%" operator and the scanf function.

These two especially the % confused me too. Lets hope I'm gonna clear things the same.

No comments: