?
Solved

Pointers

Posted on 2005-03-29
8
Medium Priority
?
263 Views
Last Modified: 2010-04-01
Hi Experts,

I was wondering if you could explain the differrent between those syntax and declaraion of pointers in C++.
I've often seen in coding that used,  char* without putting the identifier after it.
what does it mean by putting (*) after char or int. For example int* and char*?
One more question is " int * a "  the same as "int *a"? I guess it should be the same. But differrent people might used differrent syle in coding.


Thank you all for your effort in reply.


0
Comment
Question by:tinoza2004
8 Comments
 
LVL 5

Expert Comment

by:beryl666
ID: 13659231
>>One more question is " int * a "  the same as "int *a"?
yes it is the same. infact both have the type 'int*'
 

0
 
LVL 5

Assisted Solution

by:dennis_george
dennis_george earned 160 total points
ID: 13659243
Answers embedded...

>>I've often seen in coding that used,  char* without putting the identifier after it
I think this you have seen in function declarations or parameters.... e.g.

void func(char *, int);  // There is no need for providing  identifier name here... because its just a decalaration.
or
// If you are not using some arguements then also there is no neccessity for providing the name of the arguement....
void func(int num, char *)
{
}

>> what does it mean by putting (*) after char or int. For example int* and char*?
This is prototype for pointer declaration.......
int * ==> It saye pointer of type int
char * ==> It saye pointer of type char
(*) tells that its a pointer variable.....


>>> is " int * a "  the same as "int *a"?

Yes you are right both of them are same.... its just a matter of coding style........

Cheers
Dennis
0
 
LVL 5

Expert Comment

by:beryl666
ID: 13659255
it is best to explain to you with example:

#include <stdio.h>
void tryToSwap( int a, int b ) {
 int temp;
 temp = a;  a = b;  b = temp;
}
void main() {
 int a=3,b=9;
 tryToSwap(a,b);        
 printf("a = %d,  b = %d\n",a,b);// from here you will get the value a=3, b=9
}

from example above, the value of a and b has changed  in function tryToSwap( int a, int b ) but in the main funtion, it is still the same. why? because both the main function and tryToSwap( int a, int b ) has local scope which the value will be gone once exit of the function. It would be good if we can exit the function but in the same time, the value will carry to the main right?

#include <stdio.h>
void tryToSwap( int a, int b ) {
 int temp;
 temp = a;  a = b;  b = temp;
}
void main() {
 int* p1;
int* p2;
int a=3,b=9;
p1=&a;
p2=&b;
 tryToSwap(p1,p2);        
 printf("a = %d,  b = %d\n",a,b);//you will be surprise that you can get a=9 and b=3 here.
}

do you know why?
what i do this
p1=&a;// p1 which is a pointer will point to the memory location of a, that's mean when p1 changed a will change also. if a change *p1 will also change.
p2=&b;// same here
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Assisted Solution

by:beryl666
beryl666 earned 160 total points
ID: 13659286
sorry my code up there contains error: second one should be:-
#include <stdio.h>
#include<conio.h>

void tryToSwap( int&a, int& b ) {
 int temp;
 temp = a;  a = b;  b = temp;
}
int  main() {
 int* p1;
int* p2;
int a=3,b=9;
p1=&a;
p2=&b;
 tryToSwap(*p1,*p2);        
 printf("a = %d,  b = %d\n",a,b);//you will be surprise that you can get a=9 and b=3 here.
 getch();
 return 0;
}

or you may do like this:
#include <stdio.h>
void swap( int* pA, int* pB ) {
 int temp = *pA;
 *pA = *pB;  *pB = temp;
}

void main() {
 int a=3,b=9;
 swap(&a,&b);
 printf("a = %d,  b = %d\n",a,b);
}
which will get the same result. it is depend on you where to declara the pointer. either the main function or sub
0
 
LVL 5

Expert Comment

by:beryl666
ID: 13659315


>>char* without putting the identifier after it.
Do you know why we no need to put identifier?
if we do this:
int *a=4;// from here a pointer point to a unknown location and assignment the unknown location with value 3. when you want to change the value, it is dangerous! as you might changed some important data that you don't know which might make your pc data currupted! So Never Ever do this!

compare with
int *a; // you declare a pointer
int p;
a=&p;// you pointer to a known location which is reserved for p. it is safe to change the value

use of a pointer?
-> same memory space as we no need to declare and reserve a memory space for it.
if we do
int p,a; we need 8 bytes of memory space.
if we do above, we only need 4 bytes. if i am not mistaken pointer doesn't need to contain any memory space. any experts, correct me if i am wrong.
0
 
LVL 5

Expert Comment

by:beryl666
ID: 13659333
hope this make you clearer. Any other questions, feel free to ask me.
0
 
LVL 9

Accepted Solution

by:
jhshukla earned 180 total points
ID: 13662848
>> if i am not mistaken pointer doesn't need to contain any memory space. any experts, correct me if i am wrong.
Yes pointers require memory and usually it is 4 bytes which allows you to access upto 4G on most modern processors. Itanium has 64bit (=8byte pointers). I am not sure about UltraSparc.

Arrays and pointers are used almost identically. So when you create an array (int arr[10], for ex) at that time you do not require additional memory. i.e. you will use 40 bytes. but if you create an array dynamically, you will need a place to remember the address of newly allocated memory. (int *arr = new int[10];). this will require 44 bytes. The reason is this. When you allocate memory statically, the compiler knows where to place the array. So it does not waste space by using 4 more bytes. whenever it sees arr, it replaces arr with the address of the first element. If you allocate memory dynamically, the compiler does not know where the array contents will be in the memory. So it requires you, the programmer, to store the address somewhere so that it can go there and figure out things.

"it replaces arr with the address of the first element"
This is the reason why you cannot do 'arr1 = arr2;' arr1 is the address of arr1[0]. the statement is equivalent to '&arr1[0] = &arr2[0];' and as illogical as '&a = &b;' you cannot move variables around in memory.

==============================================================
>> int * ==> It saye pointer of type int
>> char * ==> It saye pointer of type char
and int ** ==> a pointer to (a pointer to an int)
and int *** ==> a pointer to (a pointer to (a pointer to an int))
and so on

int const * ==> a const pointer to an int. this kind pointer points to a fixed location in the memory. the contents of the memory can be changed but pointer cannot be changed to point to a different memory location
const int * ==> a pointer to a const int. this kind of pointer can point to any memory location, but the contents of the memory cannot be changed through the pointer. It will be clearer with this example:
int a = 10;
const int *p = &a;
*p = 2; // not allowed. compiler error
a = 2; // allowed. after this a has the value 2
const int const * ==> a combination of above two.
also you cannot assign an int pointer the address of a const int. ex:
const int a = 0;
int *p = &a; // not allowed. because *p can be modified which should not be allowed.
0
 

Author Comment

by:tinoza2004
ID: 13668925
Thank you all for reply.

These have made me clear.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question