What will be result/errors in code - practice Pointers

Ok, i have some samples and i need help. The question is what will appear on the screen after running the code; only some parts of the code are given.
Second task is - to notice what is incorrect in the code.

Thanks in advance!


P.S.

I must point out, that this isn't any kind of homework. I've got it on my class, to practice at home. I have trouble with pointers, and i would appreicate if someone can solve this with short explanation. Also i think, that this could be usefull to other beginners that maybe have the same problem.

1.Result?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
1.
int a = 10, *pointer1;
pointer1 = &a;
int *pointer2 = pointer1;
cout<< " a = " << a << "\t&a = " << &a << endl;
cout << "pointer1= " << pointer1 <<"\t *pointer1 = " << *pointer1 << "\t &pointer1 = " << &pointer1 << endl;
cout << "pointer2= " << pointer2 <<"\t *pointer2 = " << *pointer2 << "\t &pointer2 = " << &pointer2 << endl;

2.
int a = 10, b = 2;
int *pointer = &a;
*pointer = a + b;
b = a+ b;
pointer = &b;
cout << "a = " << a << "\t b = " << b << "\t *pointer = " << *pointer << endl;

3.
int a = 30;
int *pointer= &a;
*pointer= 0;
if(pointer)
cout<<*pointer<<endl;
else
cout<<"null-pointer.\n";

4.
int a = 30;
int *pointer= &a;
pointer= 0;
if(pointer)
cout<< *pointer <<endl;
else
cout<<"null-pointer.\n";


Second part- comment and find errors in code (if they exist)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
1.
int array[]={7,4,12,3,5};
for(int i=0;i<5;i++)
cout<<*(array+i)<<endl;

2.
int array[]={7,4,12,3,5};
int *pointer = array;
for(int i=0;i<5;i++)
cout<<*(pointer+i)<<endl;

3.
int array[]={7,4,12,3,5};
int *pointer = array;
for(int i=0;i<5;i++,pointer++)
cout<<*pointer<<endl;

4.
int array[]={7,4,12,3,5};
for(int i=0;i<5;i++,array++)
cout<<*array<<endl;


AND bonus question:

what is wrong in following declarations of functions for adding elements in linked list:
1
2
void addToBeginning (node *head, int newElement)
void addToEnd(node *head, int newElement)
Last edited on
I must point out, that this isn't any kind of homework. I've got it on my class, to practice at home.

I literally hear this excuse from my 4 year old on a daily basis, so I will approach the problem with you the same way I do with her; what do you think the answer is? One of us will let you know if it is right or wrong and what you might be doing wrong.

- What does '&' mean in the context of a variable?

- What is the '*' character refereed to as when not applied for multiplication?
First of all, i really have troubles with pointers and i don't want to get some answers as 'google more, read more, those are basics' etc.

Now, & is reference operator, right? So it should give adress of variable?

and * is dereference operator, it gives value of a variable that is saved on address which is pointer by that operator?

P.S. sorry for my bad english.
I'm not going to tell you to Google, I reserve that for concepts that people are in way over their head or are completely off base about.

You're doing find so far. You know what the operators do and that pointers hold addresses, I would bet that this is better then 50% of you class right now. So if:

 
a = 10; // 'a' has a value of 10 

and
 
pointer1 = &a; // 'pointer1' holds the address of 'a' 

then what happens when
 
cout << /*arbitrary code*/ << *pointer1; // we send the value stored at that address to cout? 


What gets displayed? If it seems complicated then you're over thinking it.
Last edited on
Topic archived. No new replies allowed.