Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Re: Why C has Pointers.

Reply
Thread Tools

Re: Why C has Pointers.

 
 
88888 Dihedral
Guest
Posts: n/a
 
      12-17-2011
On Wednesday, December 7, 2011 8:22:56 PM UTC+8, curixinfotech wrote:
> hi
> my Q is on the c language tell me now why c has pointers.
> parshant
>
>
> --
> curixinfotech
> ------------------------------------------------------------------------
> curixinfotech's Profile: http://forums.yourdomain.com.au/member.php?userid=134
> View this thread: http://forums.yourdomain.com.au/showthread.php?t=3319


OK, I'll explain how to use a pointer in C without any knowledge in the
hardware or assembly here.
//
int i;
int a[3]={ 1,2,3} ; // an array of 3 integers
int* ptr1; // Note the star should be right after int, not initialize
int* ptr2; // not initialized

ptr1=a; // Read this as to make ptr1 to be an alias of a.

for(i=0;i<3;i++) printf("The value of a[%d]=%d.\n", i, a[i]);
for(i=0;i<3;i++) printf("The value of ptr1[%d]=%d.\n", i, ptr1[i]);
//This is a shadow copy.
ptr1[0]=6; ptr2[1]=6;
for(i=0;i<3;i++) printf("The value of a[%d]=%d.\n", i, a[i]);
// a[0]=? a[1]=?

//Deep copy is different.
ptr2=malloc(3*sizeof(int)); // get another array from the heap
// if (ptr2==NULL) then raise an error!!!
memcpy(ptr2,ptr1,3*sizeof(int)); // make a deep copy
ptr2[0]=-6; *(ptr+2)=-6;

for(i=0;i<3;i++) printf("The value of a[%d]=%d.\n", i, a[i]);
for(i=0;i<3;i++) printf("The value of ptr1[%d]=%d.\n", i, ptr1[i]);
for(i=0;i<3;i++) printf("The value of ptr2[%d]=%d.\n", i, ptr2[i]);










 
Reply With Quote
 
 
 
 
Barry Schwarz
Guest
Posts: n/a
 
      12-17-2011
On Sat, 17 Dec 2011 08:51:33 -0800 (PST), 88888 Dihedral
<(E-Mail Removed)> wrote:

>On Wednesday, December 7, 2011 8:22:56 PM UTC+8, curixinfotech wrote:
>> hi
>> my Q is on the c language tell me now why c has pointers.
>> parshant
>>
>>
>> --
>> curixinfotech
>> ------------------------------------------------------------------------
>> curixinfotech's Profile: http://forums.yourdomain.com.au/member.php?userid=134
>> View this thread: http://forums.yourdomain.com.au/showthread.php?t=3319

>
>OK, I'll explain how to use a pointer in C without any knowledge in the
>hardware or assembly here.
>//
>int i;
>int a[3]={ 1,2,3} ; // an array of 3 integers
>int* ptr1; // Note the star should be right after int, not initialize
>int* ptr2; // not initialized
>
>ptr1=a; // Read this as to make ptr1 to be an alias of a.


If that were true, the sizeof(a) and sizeof(ptr1) would be equal
which, in the general case, they are not. ptr1 is simply a pointer
that points to the start of a. Even though they share some
similarities in the language syntax, a pointer is a completely
different type than an array. Calling one an alias of the other just
breeds unnecessary confusion.

>
>for(i=0;i<3;i++) printf("The value of a[%d]=%d.\n", i, a[i]);
>for(i=0;i<3;i++) printf("The value of ptr1[%d]=%d.\n", i, ptr1[i]);
>//This is a shadow copy.
>ptr1[0]=6; ptr2[1]=6;


Since ptr2 was never initialized or assigned a value, the second
statement invokes undefined behavior. It is probably a typo and ptr1
was intended.

ptr1 points to a. There is no copy. a[0] and ptr1[0] refer to the
exact same object.

>for(i=0;i<3;i++) printf("The value of a[%d]=%d.\n", i, a[i]);
>// a[0]=? a[1]=?
>
>//Deep copy is different.
>ptr2=malloc(3*sizeof(int)); // get another array from the heap
>// if (ptr2==NULL) then raise an error!!!
>memcpy(ptr2,ptr1,3*sizeof(int)); // make a deep copy
>ptr2[0]=-6; *(ptr+2)=-6;


Since there is no variable ptr, this cannot compile. Was *(ptr2+2)
intended?

>
>for(i=0;i<3;i++) printf("The value of a[%d]=%d.\n", i, a[i]);
>for(i=0;i<3;i++) printf("The value of ptr1[%d]=%d.\n", i, ptr1[i]);
>for(i=0;i<3;i++) printf("The value of ptr2[%d]=%d.\n", i, ptr2[i]);


a[i] and ptr1[i] still point to the same object. ptr2 points to a
different area of memory which at one time had a copy of a but has
since been assigned at least one different value.

--
Remove del for email
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      12-17-2011
88888 Dihedral <(E-Mail Removed)> writes:
> On Wednesday, December 7, 2011 8:22:56 PM UTC+8, curixinfotech wrote:
>> my Q is on the c language tell me now why c has pointers.
>> parshant

>
> OK, I'll explain how to use a pointer in C without any knowledge in the
> hardware or assembly here.
> //
> int i;
> int a[3]={ 1,2,3} ; // an array of 3 integers
> int* ptr1; // Note the star should be right after int, not initialize


Why should the * be adjacent to "int"? My own preferred style (and, I
think, that of most C programmers) is:

int *ptr1;

The syntax of declarations in C follows (more or less) the syntax of
expressions; "declaration follows use". So "int *ptr;" says that *ptr
is an int -- from which it follows that ptr is a pointer to an int,
i.e., an int*.

This layout follows the grammar more closely, and makes an (admittedly
unwise) declaration like this:

int *pointer, not_a_pointer;

easier to understand.

But there is a school of thought that prefers to put the * next to the
type name. That seems to be the usual convention in C++. That's ok if
you avoid declaring multiple variables in one declaration (which you
should probably do anyway).

Note that the same principle might suggest:

int[3] arr;

which is of course a syntax error.

> int* ptr2; // not initialized
>
> ptr1=a; // Read this as to make ptr1 to be an alias of a.


That's not necessarily a good way to think of it. Since "a" is an array
object the expression consisting of its name decays to a pointer to its
first element. That pointer value is then stored in ptr1. ptr1 points
to the first element of a. To show that it's not a true alias, consider
sizeof a vs. sizeof ptr1.

> for(i=0;i<3;i++) printf("The value of a[%d]=%d.\n", i, a[i]);
> for(i=0;i<3;i++) printf("The value of ptr1[%d]=%d.\n", i, ptr1[i]);
> //This is a shadow copy.
> ptr1[0]=6; ptr2[1]=6;
> for(i=0;i<3;i++) printf("The value of a[%d]=%d.\n", i, a[i]);
> // a[0]=? a[1]=?
>
> //Deep copy is different.
> ptr2=malloc(3*sizeof(int)); // get another array from the heap
> // if (ptr2==NULL) then raise an error!!!
> memcpy(ptr2,ptr1,3*sizeof(int)); // make a deep copy
> ptr2[0]=-6; *(ptr+2)=-6;
>
> for(i=0;i<3;i++) printf("The value of a[%d]=%d.\n", i, a[i]);
> for(i=0;i<3;i++) printf("The value of ptr1[%d]=%d.\n", i, ptr1[i]);
> for(i=0;i<3;i++) printf("The value of ptr2[%d]=%d.\n", i, ptr2[i]);


Read section 6 of the comp.lang.c FAQ, <http://c-faq.com>.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
ralph
Guest
Posts: n/a
 
      12-17-2011
On Sat, 17 Dec 2011 10:10:09 -0800, Barry Schwarz <(E-Mail Removed)>
wrote:

>On Sat, 17 Dec 2011 08:51:33 -0800 (PST), 88888 Dihedral
><(E-Mail Removed)> wrote:
>
>>On Wednesday, December 7, 2011 8:22:56 PM UTC+8, curixinfotech wrote:
>>> hi
>>> my Q is on the c language tell me now why c has pointers.
>>> parshant
>>>
>>>
>>> --
>>> curixinfotech
>>> ------------------------------------------------------------------------
>>> curixinfotech's Profile: http://forums.yourdomain.com.au/member.php?userid=134
>>> View this thread: http://forums.yourdomain.com.au/showthread.php?t=3319

>>
>>OK, I'll explain how to use a pointer in C without any knowledge in the
>>hardware or assembly here.
>>//
>>int i;
>>int a[3]={ 1,2,3} ; // an array of 3 integers
>>int* ptr1; // Note the star should be right after int, not initialize
>>int* ptr2; // not initialized
>>
>>ptr1=a; // Read this as to make ptr1 to be an alias of a.

>
>If that were true, the sizeof(a) and sizeof(ptr1) would be equal
>which, in the general case, they are not. ptr1 is simply a pointer
>that points to the start of a. Even though they share some
>similarities in the language syntax, a pointer is a completely
>different type than an array. Calling one an alias of the other just
>breeds unnecessary confusion.
>


I agree.

I have trouble understanding the constant confusion between arrays and
pointers. Way back when (and we are talking many moons ago <g>) when I
started out in C my mentor explained it very simply in these terms in
the first couple of days.

1) There is a difference between declarations and definitions.
Remember which you are dealing with and when.
2) A pointer is a "variable", an array is a "constant"
3) The array name evaluates to the address of the first element in the
array.
4) A pointer is a variable that holds the address of another variable
or an array.
5) All pointers must be intialized before you use them.
6) Since an array name is an address it can be used as a pointer that
is initialized to point to the first element in the array.
7) *variable means the contents of the variable.
&variable means the address of the variable.
9) The array operator evaluates to a storage map equation, eg.
type ar[i] := *(a + (i * sizeof(type))
10) In all "pointer" operations you are either chewing on an address
or the contents (value) at that address. Simple deconstrution of the
even the most complex expression will quickly reveal which it is.

I've since learned how to restate the above in fancier terminology,
but those simple rules have stuck and served me well from my second
week in C to today - thirty plus years later.

-ralph
 
Reply With Quote
 
ralph
Guest
Posts: n/a
 
      12-17-2011
On Sat, 17 Dec 2011 14:28:14 -0600, ralph <(E-Mail Removed)>
wrote:
I should probably amplify #1 ...

>
>1) There is a difference between declarations and definitions.
>Remember which you are dealing with and when.


1.5) C operators are overloaded to some extent and often a different
meaning in a declaration opposed to a definition or expression. For
example:
*v means the contents (value) of the variable pointed to by v in an
expression, but
char* v means declare a pointer that holds the address of a
variable that holds a char in a declaration.

....

-ralph
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      12-17-2011
ralph <(E-Mail Removed)> writes:
[...]
> I have trouble understanding the constant confusion between arrays and
> pointers. Way back when (and we are talking many moons ago <g>) when I
> started out in C my mentor explained it very simply in these terms in
> the first couple of days.
>
> 1) There is a difference between declarations and definitions.
> Remember which you are dealing with and when.
> 2) A pointer is a "variable", an array is a "constant"


That doesn't really make sense. An array variable is not a constant:

int arr[10];
arr[5] = 42;

I suppose what it means is that an array name, when it appears in an
expression (in most but not all context) decays to a pointer to the
array's first element, and that pointer value cannot be modified. But I
think that saying an array is a constant just reinforces the confusion
between arrays and pointers.

> 3) The array name evaluates to the address of the first element in the
> array.


This applies to any expression of array type, not just the name of a
declared array object. But it doesn't apply in all contexts.
Specifically, an expression of array type is converted to a pointer to
the array object's first element *unless* it's:

The operand of a unary "sizeof" operator (sizeof arr gives you the
same of the array, not of a pointer);

The operand of a unary "&" operator (&arr gives you the address of
the array object; it's the same memory location as the address of
its first element, but it's of a different type);

A string literal in an initializer used to initialize an array
object. In
char arr[6] = "hello";
the *value* of "hello" is copied into arr.

> 4) A pointer is a variable that holds the address of another variable
> or an array.


The distinction between variables an arrays is unnecessary. Variables
can be of any object type: integer, floating-point, struct, union,
array, pointer. There's nothing special about arrays in that particular
context.

An array object is an object whose value consists of the values of its
elements, just as a struct object is an object whose value consists of
the values of its members.

> 5) All pointers must be intialized before you use them.


The same applies to any object.

> 6) Since an array name is an address it can be used as a pointer that
> is initialized to point to the first element in the array.


In most contexts; see above. It would be clearer to say that an array
name *decays to* an address (or pointer value). And I wouldn't use the
word "initialized".

> 7) *variable means the contents of the variable.


No, it means the contents of the object to which "variable" points (and
is valid only if "variable" is of pointer type).

> &variable means the address of the variable.
> 9) The array operator evaluates to a storage map equation, eg.
> type ar[i] := *(a + (i * sizeof(type))


I'm not sure what syntax you're using. What does "type" mean here? And
C pointer arithmetic is already scaled by the size of the pointed-to
type.

arr[i] is by definition equivalent to *(arr + i).

> 10) In all "pointer" operations you are either chewing on an address
> or the contents (value) at that address. Simple deconstrution of the
> even the most complex expression will quickly reveal which it is.
>
> I've since learned how to restate the above in fancier terminology,
> but those simple rules have stuck and served me well from my second
> week in C to today - thirty plus years later.


Section 6 of the comp.lang.c FAQ, <http://c-faq.com>, has an excellent
explanation of the relationship between arrays and pointers.

A digression: The rule that an array expression decays into a pointer to
the array object's first element leads to an interesting little hole in
the language. It assumes that there is an array *object* to which you
can point. This is usually the case, but not always. For example:

#include <stdio.h>

struct foo {
int arr[3];
};

struct foo func(void) {
struct foo result = { { 10, 20, 30 } };
return result;
}

int main(void) {
int *ptr = func().arr;
printf("*ptr = %d\n", *ptr);
return 0;
}

func() returns a *value* of type struct foo; it doesn't refer to any
object ("result" ceases to exist when the function returns). So we have
an array value, a member of the struct value returned by func(), that is
not the value of any array object.

C201X addresses this.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
88888 Dihedral
Guest
Posts: n/a
 
      12-17-2011
On Sunday, December 18, 2011 3:45:14 AM UTC+8, Keith Thompson wrote:
> 88888 Dihedral <(E-Mail Removed)> writes:
> > On Wednesday, December 7, 2011 8:22:56 PM UTC+8, curixinfotech wrote:
> >> my Q is on the c language tell me now why c has pointers.
> >> parshant

> >
> > OK, I'll explain how to use a pointer in C without any knowledge in the
> > hardware or assembly here.
> > //
> > int i;
> > int a[3]={ 1,2,3} ; // an array of 3 integers
> > int* ptr1; // Note the star should be right after int, not initialize

>
> Why should the * be adjacent to "int"? My own preferred style (and, I
> think, that of most C programmers) is:
>
> int *ptr1;
>

int** (*methods[ ])() ;

How do you read this?

int* (*methods2[ ])() ;

int* (*methods3[ ])( int* (*functor)(), int* ) ;

methods, methods2, and methods3 are different arrays of functors.

It is easy to use functors to perform tricks in functional programming.



 
Reply With Quote
 
88888 Dihedral
Guest
Posts: n/a
 
      12-17-2011
On Sunday, December 18, 2011 4:28:14 AM UTC+8, ralph wrote:
> On Sat, 17 Dec 2011 10:10:09 -0800, Barry Schwarz <(E-Mail Removed)>
> wrote:
>
> >On Sat, 17 Dec 2011 08:51:33 -0800 (PST), 88888 Dihedral
> ><(E-Mail Removed)> wrote:
> >
> >>On Wednesday, December 7, 2011 8:22:56 PM UTC+8, curixinfotech wrote:
> >>> hi
> >>> my Q is on the c language tell me now why c has pointers.
> >>> parshant
> >>>
> >>>
> >>> --
> >>> curixinfotech
> >>> ------------------------------------------------------------------------
> >>> curixinfotech's Profile: http://forums.yourdomain.com.au/member.php?userid=134
> >>> View this thread: http://forums.yourdomain.com.au/showthread.php?t=3319
> >>
> >>OK, I'll explain how to use a pointer in C without any knowledge in the
> >>hardware or assembly here.
> >>//
> >>int i;
> >>int a[3]={ 1,2,3} ; // an array of 3 integers
> >>int* ptr1; // Note the star should be right after int, not initialize
> >>int* ptr2; // not initialized
> >>
> >>ptr1=a; // Read this as to make ptr1 to be an alias of a.

> >
> >If that were true, the sizeof(a) and sizeof(ptr1) would be equal
> >which, in the general case, they are not. ptr1 is simply a pointer
> >that points to the start of a. Even though they share some
> >similarities in the language syntax, a pointer is a completely
> >different type than an array. Calling one an alias of the other just
> >breeds unnecessary confusion.
> >

>
> I agree.
>
> I have trouble understanding the constant confusion between arrays and
> pointers. Way back when (and we are talking many moons ago <g>) when I
> started out in C my mentor explained it very simply in these terms in
> the first couple of days.
>
> 1) There is a difference between declarations and definitions.
> Remember which you are dealing with and when.
> 2) A pointer is a "variable", an array is a "constant"
> 3) The array name evaluates to the address of the first element in the
> array.
> 4) A pointer is a variable that holds the address of another variable
> or an array.
> 5) All pointers must be intialized before you use them.
> 6) Since an array name is an address it can be used as a pointer that
> is initialized to point to the first element in the array.
> 7) *variable means the contents of the variable.


*variable is easy to be miss-understood

*ptr_variable is different from ptr_variable

*variable if used for non-pointer variables, the compiler
will say something, thus you are right in the general sense in C,
but not clear enough for novices


 
Reply With Quote
 
ralph
Guest
Posts: n/a
 
      12-18-2011
On Sat, 17 Dec 2011 13:55:44 -0800, Keith Thompson <(E-Mail Removed)>
wrote:

>ralph <(E-Mail Removed)> writes:
>[...]
>> I have trouble understanding the constant confusion between arrays and
>> pointers. Way back when (and we are talking many moons ago <g>) when I
>> started out in C my mentor explained it very simply in these terms in
>> the first couple of days.
>>
>> 1) There is a difference between declarations and definitions.
>> Remember which you are dealing with and when.
>> 2) A pointer is a "variable", an array is a "constant"

>
>That doesn't really make sense. An array variable is not a constant:
>
> int arr[10];
> arr[5] = 42;
>
>I suppose what it means is that an array name, when it appears in an
>expression (in most but not all context) decays to a pointer to the
>array's first element, and that pointer value cannot be modified. But I
>think that saying an array is a constant just reinforces the confusion
>between arrays and pointers.
>


I disagree.

Once the beginner gets it through his head that you can never do an
assignment to 'arr' you resolve a ton of the questions we get around
here. That as close to the definition of a "constant" as you can get.

-ralph


>> 3) The array name evaluates to the address of the first element in the
>> array.

>
>This applies to any expression of array type, not just the name of a
>declared array object. But it doesn't apply in all contexts.
>Specifically, an expression of array type is converted to a pointer to
>the array object's first element *unless* it's:
>
> The operand of a unary "sizeof" operator (sizeof arr gives you the
> same of the array, not of a pointer);
>
> The operand of a unary "&" operator (&arr gives you the address of
> the array object; it's the same memory location as the address of
> its first element, but it's of a different type);
>
> A string literal in an initializer used to initialize an array
> object. In
> char arr[6] = "hello";
> the *value* of "hello" is copied into arr.
>


Yep. But what's the big deal.

And of course I disagree that it actually matters to the beginner that
the address is of a "different type". Semantically and syntactically
it comes out the same. Plenty of time to get into vague language
later.

Actually in some implementations whether "hello" gets copied and when
if gets copied if it does depends on context. Again, why add
confusion?

[You can also create snippets that demonstrate that "hello" are
identical "constants" ...
"hello"[3]; \\ 'l'
arr[3]; \\ 'l'


>> 4) A pointer is a variable that holds the address of another variable
>> or an array.

>
>The distinction between variables an arrays is unnecessary. Variables
>can be of any object type: integer, floating-point, struct, union,
>array, pointer. There's nothing special about arrays in that particular
>context.
>
>An array object is an object whose value consists of the values of its
>elements, just as a struct object is an object whose value consists of
>the values of its members.
>
>> 5) All pointers must be intialized before you use them.

>
>The same applies to any object.
>
>> 6) Since an array name is an address it can be used as a pointer that
>> is initialized to point to the first element in the array.

>
>In most contexts; see above. It would be clearer to say that an array
>name *decays to* an address (or pointer value). And I wouldn't use the
>word "initialized".
>


I would. And again what does 'decay' meant to the beginner?
In code the name "arr" is changed to, or evaluates to, the address of
the first element.

>> 7) *variable means the contents of the variable.

>
>No, it means the contents of the object to which "variable" points (and
>is valid only if "variable" is of pointer type).
>
>> &variable means the address of the variable.
>> 9) The array operator evaluates to a storage map equation, eg.
>> type ar[i] := *(a + (i * sizeof(type))

>
>I'm not sure what syntax you're using. What does "type" mean here? And
>C pointer arithmetic is already scaled by the size of the pointed-to
>type.
>
>arr[i] is by definition equivalent to *(arr + i).
>


Think about it.

You say that now, but you come back later in other response to point
out "sizes". That "already scaled" is because the compiler knows the
type, and he knows the type because you declared it.


>> 10) In all "pointer" operations you are either chewing on an address
>> or the contents (value) at that address. Simple deconstrution of the
>> even the most complex expression will quickly reveal which it is.
>>
>> I've since learned how to restate the above in fancier terminology,
>> but those simple rules have stuck and served me well from my second
>> week in C to today - thirty plus years later.

>
>Section 6 of the comp.lang.c FAQ, <http://c-faq.com>, has an excellent
>explanation of the relationship between arrays and pointers.
>
>A digression: The rule that an array expression decays into a pointer to
>the array object's first element leads to an interesting little hole in
>the language. It assumes that there is an array *object* to which you
>can point. This is usually the case, but not always. For example:
>
>#include <stdio.h>
>
>struct foo {
> int arr[3];
>};
>
>struct foo func(void) {
> struct foo result = { { 10, 20, 30 } };
> return result;
>}
>
>int main(void) {
> int *ptr = func().arr;
> printf("*ptr = %d\n", *ptr);
> return 0;
>}
>
>func() returns a *value* of type struct foo; it doesn't refer to any
>object ("result" ceases to exist when the function returns). So we have
>an array value, a member of the struct value returned by func(), that is
>not the value of any array object.
>
>C201X addresses this.

 
Reply With Quote
 
ralph
Guest
Posts: n/a
 
      12-18-2011
On Sat, 17 Dec 2011 15:35:49 -0800 (PST), 88888 Dihedral
<(E-Mail Removed)> wrote:

>On Sunday, December 18, 2011 4:28:14 AM UTC+8, ralph wrote:
>> On Sat, 17 Dec 2011 10:10:09 -0800, Barry Schwarz <(E-Mail Removed)>
>> wrote:
>>
>> >On Sat, 17 Dec 2011 08:51:33 -0800 (PST), 88888 Dihedral
>> ><(E-Mail Removed)> wrote:
>> >
>> >>On Wednesday, December 7, 2011 8:22:56 PM UTC+8, curixinfotech wrote:
>> >>> hi
>> >>> my Q is on the c language tell me now why c has pointers.
>> >>> parshant
>> >>>
>> >>>
>> >>> --
>> >>> curixinfotech
>> >>> ------------------------------------------------------------------------
>> >>> curixinfotech's Profile: http://forums.yourdomain.com.au/member.php?userid=134
>> >>> View this thread: http://forums.yourdomain.com.au/showthread.php?t=3319
>> >>
>> >>OK, I'll explain how to use a pointer in C without any knowledge in the
>> >>hardware or assembly here.
>> >>//
>> >>int i;
>> >>int a[3]={ 1,2,3} ; // an array of 3 integers
>> >>int* ptr1; // Note the star should be right after int, not initialize
>> >>int* ptr2; // not initialized
>> >>
>> >>ptr1=a; // Read this as to make ptr1 to be an alias of a.
>> >
>> >If that were true, the sizeof(a) and sizeof(ptr1) would be equal
>> >which, in the general case, they are not. ptr1 is simply a pointer
>> >that points to the start of a. Even though they share some
>> >similarities in the language syntax, a pointer is a completely
>> >different type than an array. Calling one an alias of the other just
>> >breeds unnecessary confusion.
>> >

>>
>> I agree.
>>
>> I have trouble understanding the constant confusion between arrays and
>> pointers. Way back when (and we are talking many moons ago <g>) when I
>> started out in C my mentor explained it very simply in these terms in
>> the first couple of days.
>>
>> 1) There is a difference between declarations and definitions.
>> Remember which you are dealing with and when.
>> 2) A pointer is a "variable", an array is a "constant"
>> 3) The array name evaluates to the address of the first element in the
>> array.
>> 4) A pointer is a variable that holds the address of another variable
>> or an array.
>> 5) All pointers must be intialized before you use them.
>> 6) Since an array name is an address it can be used as a pointer that
>> is initialized to point to the first element in the array.
>> 7) *variable means the contents of the variable.

>
> *variable is easy to be miss-understood
>
> *ptr_variable is different from ptr_variable
>
> *variable if used for non-pointer variables, the compiler
>will say something, thus you are right in the general sense in C,
>but not clear enough for novices
>


I disagree. (Naturally. <g>)

This ...
> *ptr_variable is different from ptr_variable


is all about context.

You only get an error or in trouble if you use *variable on a
NON-Address - whether constant or 'variable'.

I firmly believe, and several years of teaching C confirms for me,
that it is when ones starts straying from the simple that confusion is
introduced.

-ralph

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Cisco 2611 and Cisco 1721 : Why , why , why ????? sam@nospam.org Cisco 10 05-01-2005 08:49 AM
Why, why, why??? =?Utf-8?B?VGltOjouLg==?= ASP .Net 6 01-27-2005 03:35 PM
Why Why Why You HAVE NO IDEA MCSE 31 04-24-2004 06:40 PM



Advertisments