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

>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:

int * const a = init; // Const pointer to int
const int * a; // Pointer to const int
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:

extern float* const c[]; // OK! The declaration is extern and thus, incomplete

// OK! The array now has a size and an initializer
float* const c[] = {
  &init, &init, &init
};

>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".

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

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

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? ).

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~^^

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 :(

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!...

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........:idea:

thank you

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 :

#include <stdio.h>

int main()
{
int num,number[100],i=0,j,divider;

double average=0,sum=0;

	do
	{
		
		printf("please input a number :");
		scanf("%d",&num);
		printf("%d\n",num);
		number[i]=num;
		sum=sum+number[i];
		i++;
	}while (num!=-1);

	sum=sum+1;
	printf("The sum is : %d\n",sum);
	average=sum/(i-1);
	printf("The average is :%f",average);
	return 0;

}

but i have assigned the size of the array in this case.

This article has been dead for over six months. Start a new discussion instead.