what are the meaning of those declarations?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2008
Posts: 4
Reputation: adrian116 is an unknown quantity at this point 
Solved Threads: 0
adrian116 adrian116 is offline Offline
Newbie Poster

what are the meaning of those declarations?

 
0
  #1
Oct 10th, 2008
1. const int* a; (what the difference between const int *a and why pointer need to be const?)
2. short b[10]; (is the content of b are all short type?)
3. float* const c[]; (i think this should be wrong due to the size of array c is not assigned)
4. unsigned int (*d)(const float*); (i totally don't have any idea.....)
5. int **e; (is e the pointer of pointer?)

thank you
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,844
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 752
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: what are the meaning of those declarations?

 
1
  #2
Oct 10th, 2008
>1. const int* a; (what the difference between const int *a and why pointer need to be const?)
Whitespace makes no difference here. const int* a and const int *a are identical. The pointer is not const, it points to an object that is const. To make a pointer const, you apply the const keyword after the asterisk:
  1. int * const a = init; // Const pointer to int
  2. const int * a; // Pointer to const int
  3. const int * const a = init; // Const pointer to const int
>2. short b[10]; (is the content of b are all short type?)
Yes.

>3. float* const c[]; (i think this should be wrong due to the size of array c is not assigned)
If you make the declaration extern, it'll be okay. Otherwise you have to initialize the array because the member pointers are const:
  1. extern float* const c[]; // OK! The declaration is extern and thus, incomplete
  2.  
  3. // OK! The array now has a size and an initializer
  4. float* const c[] = {
  5. &init, &init, &init
  6. };
>4. unsigned int (*d)(const float*); (i totally don't have any idea.....)
It's a pointer to a function that takes a pointer to const float and returns an unsigned int.

>5. int **e; (is e the pointer of pointer?)
It is indeed. The usual terminology is "e is a pointer to a pointer to int".
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 114
Reputation: sidatra79 is an unknown quantity at this point 
Solved Threads: 8
sidatra79's Avatar
sidatra79 sidatra79 is offline Offline
Junior Poster

Re: what are the meaning of those declarations?

 
0
  #3
Oct 10th, 2008
Concerning the first I would like to add:
const int*a;
and
const int *a;
are the same.
and are read: a is a pointer to a constant int.
----
We need a constant pointer when we need that a pointer once initialized it cannot be pointed to anything else->works like a reference

the rest were all covered in the previous post


Originally Posted by adrian116 View Post
1. const int* a; (what the difference between const int *a and why pointer need to be const?)
2. short b[10]; (is the content of b are all short type?)
3. float* const c[]; (i think this should be wrong due to the size of array c is not assigned)
4. unsigned int (*d)(const float*); (i totally don't have any idea.....)
5. int **e; (is e the pointer of pointer?)

thank you
Last edited by sidatra79; Oct 10th, 2008 at 10:08 am.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: what are the meaning of those declarations?

 
0
  #4
Oct 10th, 2008
1. It's not a constant pointer - it's a pointer to const int. You can't modify referred int value via this pointer.
2. All 10 elements of array b are short int.
3. It's a function parameter declarator (the only context where this declaration occured w/o extern prefix or initializer suffix). It declares constant pointer to array of pointers to floats. You can't modify this pointer parameter (but can modify an array of pointers via this pointer).
4. It's a declaration of a pointer to a function with const float* parameter (see #1 declaration) which returns unsigned int value.
5. It's a pointer to a pointer to int (yes, it's a pointer to pointer - why not? ).
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 4
Reputation: adrian116 is an unknown quantity at this point 
Solved Threads: 0
adrian116 adrian116 is offline Offline
Newbie Poster

Re: what are the meaning of those declarations?

 
0
  #5
Oct 10th, 2008
THank for all of your.

i have some other questions.

1) if i want to write a program that reads integer numbers from the user until it receives a '-1' as input. After this, it should print out the average value of all read numbers. There is no limit to the number of given input values. I have finished all the function except to make no limit to the number of given input values. I save the input value into a array. How can i save no limit value into an array?

2.) I was given the following declarations,

struct S1
{
char c[4];
char *s;
int i;
} s1 = {"abc", "def", 2};

struct S1 *ps;

struct S1 as[] = {{"abc", "q", 0},{"def", "*zy", 1},{"ghi", "rst", 2}};

struct S2
{
int j;
struct S1 ss;
} s2 = {37, {"ghi", "j", 8}};

what is the type of each of the following expressions?
I have most of them but not sure if it is correct.
s1 -----S1
ps -----S1
as -----S1
s1.c ---char
s1.s ---char
s2.j ---int
s2.ss --S1
*ps->s ----?

Thank you again~^^
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 273
Reputation: Sci@phy will become famous soon enough Sci@phy will become famous soon enough 
Solved Threads: 42
Sci@phy's Avatar
Sci@phy Sci@phy is offline Offline
Posting Whiz in Training

Re: what are the meaning of those declarations?

 
0
  #6
Oct 10th, 2008
s1 -----S1
s1 is instance of S1.

ps -----S1
ps is declared as struct S1 *ps; , but since this is C++, not C it should be:
S1 *ps; (because whenever you make class or struct you make a new object, kind of new variable that goes by the name, not by "struct name")

But back on question, well, try to guess it?
S1 *ps;
Yes, it's a pointer that points to S1 type.

as -----S1
as is declared as S1 as[] = ... So as is array of S1's (same as int as[] = {1, 2, 3, 4})

s1.c ---char
Pretty close, but not. Variable c inside s1 (and remember, s1 is an instance of S1!) is char c[4];
So s1.c is array of chars. It's size is 4 chars.

s1.s ---char
Same again. s1.s is pointer to type char.

s2.j ---int
That's right!

s2.ss --S1
Correct again.

*ps->s ----?
Ok, this is sort of nice. The better (more readable) syntax is: *(ps->s) So, what is "ps->s"? It's the same as (*ps).s so entire line is equivalent to:
*( (*ps).s ); Figure out what that is

But this seems like some homework assignment... And I just solved it to you
Last edited by Sci@phy; Oct 10th, 2008 at 1:03 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: what are the meaning of those declarations?

 
0
  #7
Oct 10th, 2008
Of course, it's a homework questions.
So the answer to #1: where is your code? What for you accumulate input values into an array if you "should print out the average value of all read numbers"?
#2: Oh poor, poor Sci@phy What's a craftiness!...
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 4
Reputation: adrian116 is an unknown quantity at this point 
Solved Threads: 0
adrian116 adrian116 is offline Offline
Newbie Poster

Re: what are the meaning of those declarations?

 
0
  #8
Oct 10th, 2008
Originally Posted by Sci@phy View Post
s1 -----S1

*ps->s ----?
Ok, this is sort of nice. The better (more readable) syntax is: *(ps->s) So, what is "ps->s"? It's the same as (*ps).s so entire line is equivalent to:
*( (*ps).s ); Figure out what that is

But this seems like some homework assignment... And I just solved it to you
For the last question. Is that the pointer of pointer s with type char?

those questions are my assignment, but some of the syntax i have never seen........

thank you
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 4
Reputation: adrian116 is an unknown quantity at this point 
Solved Threads: 0
adrian116 adrian116 is offline Offline
Newbie Poster

Re: what are the meaning of those declarations?

 
0
  #9
Oct 10th, 2008
Originally Posted by ArkM View Post
Of course, it's a homework questions.
So the answer to #1: where is your code? What for you accumulate input values into an array if you "should print out the average value of all read numbers"?
#2: Oh poor, poor Sci@phy What's a craftiness!...
Hello ArkM~

The code i have done is :
  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5. int num,number[100],i=0,j,divider;
  6.  
  7. double average=0,sum=0;
  8.  
  9. do
  10. {
  11.  
  12. printf("please input a number :");
  13. scanf("%d",&num);
  14. printf("%d\n",num);
  15. number[i]=num;
  16. sum=sum+number[i];
  17. i++;
  18. }while (num!=-1);
  19.  
  20. sum=sum+1;
  21. printf("The sum is : %d\n",sum);
  22. average=sum/(i-1);
  23. printf("The average is :%f",average);
  24. return 0;
  25.  
  26. }
but i have assigned the size of the array in this case.
Last edited by cscgal; Oct 10th, 2008 at 10:36 pm. Reason: Added code tags
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC