float ** and float *p[]

V

vjnr83

Hi,

I have a doubt:

what is the difference between float **p and float *p[10]?

Thanks in advance,
Vijay
 
R

Roger

(e-mail address removed) wrote in @n67g2000cwd.googlegroups.com:
Hi,

I have a doubt:

Me too ;)
what is the difference between float **p and float *p[10]?

The first is a pointer to a pointer. The second is an array of 10
pointers.

Now, what do you really want to know?

-R
 
M

mark_bluemel

(e-mail address removed) wrot
what is the difference between float **p and float *p[10]?

Lots...

The first specifies that the variable "p" is a pointer,which when
resolved would point in turn to a pointer, which would in turn point to
a data item of "float" type. It is your job to make sure both the
second pointer and the float type exist...

The second specifies that "p" is an array of ten items each of which is
a pointer to a float data type .

I suggest you go to the FAQ and read http://c-faq.com/aryptr/index.html
and the pages pointed to from it.
 
C

CoL

Hi ,
float **p is moreover a generic form of the later one float *p[].
The second one float *p[] which is array of 10 pointers to float -- you
generally use
when you are sure about the SIIZE of your requirement.
And the first float **p which is obviously pointer ->pointer
essentially same as first --- in a more flexible form .

An example of float **p----
a) float **p=(float**) malloc(sizeof(*float)*n); //where n is some size
known at run time
now---
b) for(i=0;i<n;i++)
p=(float*)malloc(sizeof(float));
Now you can store valures in ps.

Step (a) is not required in the later case float *p[10] as you already
defined its size to 10.
Just second step (b) and move ahead.

Regards
COL
 
S

santosh

CoL said:
Hi ,
float **p is moreover a generic form of the later one float *p[].
The second one float *p[] which is array of 10 pointers to float -- you
generally use
when you are sure about the SIIZE of your requirement.
And the first float **p which is obviously pointer ->pointer
essentially same as first --- in a more flexible form .

An example of float **p----
a) float **p=(float**) malloc(sizeof(*float)*n); //where n is some size
known at run time

That can be better written as:
float **p = malloc(n * sizeof *p)
now---
b) for(i=0;i<n;i++)
p=(float*)malloc(sizeof(float));


Again: p = malloc(n * sizeof(float));
Now you can store valures in ps.

Step (a) is not required in the later case float *p[10] as you already
defined its size to 10.
Just second step (b) and move ahead.


An important difference is that in float **p, p can be modifiable while
in float *p[], p is non-modifiable.
 
P

pete

santosh said:
Hi ,
float **p is moreover a generic form of the later one float *p[].
The second one float *p[]
which is array of 10 pointers to float -- you generally use
when you are sure about the SIIZE of your requirement.
And the first float **p which is obviously pointer ->pointer
essentially same as first --- in a more flexible form .

An example of float **p----
a) float **p=(float**) malloc(sizeof(*float)*n);
That can be better written as:
float **p = malloc(n * sizeof *p)
now---
b) for(i=0;i<n;i++)
p=(float*)malloc(sizeof(float));


Again: p = malloc(n * sizeof(float));


ITYM p = malloc(k * sizeof *p);

There's not really any information given
to guess how many floats can be accessed through p.
 
B

Barry Schwarz

Hi ,
float **p is moreover a generic form of the later one float *p[].

Only under a very limited set of circumstances. You have totally
ignored several significant distinctions:

One is an aggregate and the other is a scalar.

One is a modifiable lvalue and the other is not.

The sizeof operator will usually return radically different
results when applied to each.
The second one float *p[] which is array of 10 pointers to float -- you
generally use
when you are sure about the SIIZE of your requirement.
And the first float **p which is obviously pointer ->pointer
essentially same as first --- in a more flexible form .

Not even close. Think about the situation where you want a function
to modify a float* that resides in the calling function.
An example of float **p----
a) float **p=(float**) malloc(sizeof(*float)*n); //where n is some size
known at run time

Don't cast the return from malloc.
now---
b) for(i=0;i<n;i++)
p=(float*)malloc(sizeof(float));
Now you can store valures in ps.


A singularly uninstructive example. If each p was intended to
point to a single float then a float** is completely unnecessary.
Step (a) is not required in the later case float *p[10] as you already
defined its size to 10.
Just second step (b) and move ahead.

Equally uninstructive.

And while you are at it, please don't top post.
Regards
COL

Hi,

I have a doubt:

what is the difference between float **p and float *p[10]?

Thanks in advance,
Vijay


Remove del for email
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top