Re: Array and Pointer Tutorial
- From: "Chad" <cdalten@xxxxxxxxx>
- Date: 11 May 2006 07:13:01 -0700
As a matter of convenient maintenance, not a true error, it is more
dabble-proof to use sizeof *pointer rather than sizeof (type). If your
pointer's type changes, the first form stays correct, the second can
turn deceptively (and hiddenly!) broken.
All in all, that program should've looked like this:
#include <stdlib.h>
int main(void)
{
int array[5];
int* const pointer = malloc(5 * sizeof *pointer);
}
Anyway, the difference between pointers and arrays is most simply
demonstrated using the age[1]-old method of arrows and groups of boxes.
Richard
[1] I.e., in the computing world, a couple of decades
Okay, I'm probably missing this. But say I have the following:
/*I omitted checking for NULL and using free*/
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int array[5];
int *q = malloc(sizeof *array);
return 0;
}
Now, I change
int array[5];
to
double array[5];
Wouldn't the sizeof double be truncated to the sizeof int? If so, the
wouldn't this create an additional bug?
Chad
.
- Follow-Ups:
- Re: Array and Pointer Tutorial
- From: Flash Gordon
- Re: Array and Pointer Tutorial
- From: CBFalconer
- Re: Array and Pointer Tutorial
- References:
- Re: Array and Pointer Tutorial
- From: Richard Bos
- Re: Array and Pointer Tutorial
- Prev by Date: Re: Boost process and C
- Next by Date: Re: getchar() problem
- Previous by thread: Re: Array and Pointer Tutorial
- Next by thread: Re: Array and Pointer Tutorial
- Index(es):
Relevant Pages
|