array vs pointer 
Author Message
 array vs pointer

Now that I feel I have a good grasp on pointers, I have to say I don't see
much of a use for arrays. It seems like an array is just a limited form of a
pointer, because it's address cant be changed, but otherwise is identical to
a pointer (assuming you allocate memory correctly). Am I correct, or do
there exist situations where it is actually advantagous to use an array in
place of a pointer?


Wed, 19 May 2004 02:48:33 GMT  
 array vs pointer

Quote:
> Now that I feel I have a good grasp on pointers, I have to say I don't
> see much of a use for arrays. It seems like an array is just a limited
> form of a pointer, because it's address cant be changed, but otherwise
> is identical to a pointer (assuming you allocate memory correctly). Am
> I correct, or do there exist situations where it is actually
> advantagous to use an array in place of a pointer?

An array is nice because you can:

1) Know the amount of memory it takes at compile/link time.
2) If it is an automatic variable, it is allocated and deallocated
   automatically.
3) You can take the sizeof an array
4) You cannot change the addres of an array (this can be a plus)

Also, a pointer is not identical to an array. An array name degrades to a
pointer to the first element but a pointer to that same element actually
exists elsewhere. E.g.

int arr[10] = { 101 };
int *pArr = arr;

Say arr allocated at address 0xBEEF_C0DE
Say pArr lives at address 0xDEAD_C0DE, pArr will contain the value
0xBEEF_C0DE, which when dereferenced will point to the value 101.

--
- Mark A. Odell
- Embedded Firmware Design, Inc.
- http://www.embeddedfw.com



Wed, 19 May 2004 02:59:40 GMT  
 array vs pointer

Quote:

> Now that I feel I have a good grasp on pointers, I have to say I don't see
> much of a use for arrays. It seems like an array is just a limited form of a
> pointer, because it's address cant be changed, but otherwise is identical to
> a pointer (assuming you allocate memory correctly). Am I correct, or do
> there exist situations where it is actually advantagous to use an array in
> place of a pointer?

Arrays and pointers are different, with complementary purposes.
Without arrays, there would be little need for pointer
arithmetic.  Pointers let you efficiently access arbitrary array
elements.

If you haven't read the C FAQ yet, it is time.
--
"The fact that there is a holy war doesn't mean that one of the sides
 doesn't suck - usually both do..."
--Alexander Viro



Wed, 19 May 2004 02:53:01 GMT  
 array vs pointer

Quote:

> Now that I feel I have a good grasp on pointers, I have to say I don't see
> much of a use for arrays. It seems like an array is just a limited form of a
> pointer, because it's address cant be changed, but otherwise is identical to
> a pointer (assuming you allocate memory correctly). Am I correct, or do
> there exist situations where it is actually advantagous to use an array in
> place of a pointer?

Mushu...

You can't input a line of text into a pointer. Are you planning to malloc()
/all/ string areas? How would you convert a month number into a month name or a
day-of-the-week number into a day name?
--
Morris Dovey
West Des Moines, Iowa USA



Wed, 19 May 2004 03:01:58 GMT  
 array vs pointer

Quote:

>> Now that I feel I have a good grasp on pointers, I have to say I don't see
>> much of a use for arrays. It seems like an array is just a limited form of a
>> pointer, because it's address cant be changed, but otherwise is identical to
>> a pointer (assuming you allocate memory correctly). Am I correct, or do
>> there exist situations where it is actually advantagous to use an array in
>> place of a pointer?
> Mushu...
> You can't input a line of text into a pointer. Are you planning to malloc()
> /all/ string areas? How would you convert a month number into a month name or a
> day-of-the-week number into a day name?

This reminds me... About a bit over a year ago, I wasn't aware that
arrays and pointers are interchangable as rvalues (but not as lvalues).
Because of this, I only ever used arrays with the [] operator, and if
I needed to pass a memory address to a function, I always malloc()ed
the storage, even if the size was constant.
Nowadays, thanks to partipication here on comp.lang.c, I am wiser, and
I always use arrays if the storage is of constant size and is always
present.

--

| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste       W++ B OP+                     |
\----------------------------------------- Finland rules! ------------/
"And according to Occam's Toothbrush, we only need to optimise the most frequent
instructions."
   - Teemu Kerola



Wed, 19 May 2004 03:34:12 GMT  
 array vs pointer

Quote:

> This reminds me... About a bit over a year ago, I wasn't aware that
> arrays and pointers are interchangable as rvalues (but not as lvalues).
> Because of this, I only ever used arrays with the [] operator, and if
> I needed to pass a memory address to a function, I always malloc()ed
> the storage, even if the size was constant.
> Nowadays, thanks to partipication here on comp.lang.c, I am wiser, and
> I always use arrays if the storage is of constant size and is always
> present.

Joona...

Interesting. When I began using C, I used arrays almost exclusively - and avoided
malloc() as much as possible; then gradually shifted to using pointers /almost/
exclusively (except for declarations and definitions) and used malloc() much more.

As a result of working with "always on" systems with a limited number of fixed-sized
object types (but large numbers of each), I've been experimenting with maintaining a
recycling list for each size object. When I need a new instance of one of the
objects, I first check to see if there an unused one on the appropriate recycling
list; and malloc() only if the list is empty. Then, instead of free()ing objects when
no longer needed, I put 'em on the appropriate queue for re-use later. This is all
fairly straight foreward; but becomes more interesting when deciding how to deal with
the situation when malloc() no longer has memory available to allocate. In the most
interesting of these systems to date, a malloc() failure triggers configuration of
another machine on the network to perform the same function and transfer of half of
the data to the new machine - which, of course, makes more entries in the recycling
list on the original machine and allows a retry of the original request for an empty
object to succeed.

(Much more than you ever wanted to know 8-)
--
Morris Dovey
West Des Moines, Iowa USA



Wed, 19 May 2004 04:12:19 GMT  
 array vs pointer

Quote:

> Now that I feel I have a good grasp on pointers, I have to say I don't see
> much of a use for arrays. It seems like an array is just a limited form of a
> pointer, because it's address cant be changed, but otherwise is identical to
> a pointer (assuming you allocate memory correctly). Am I correct, or do
> there exist situations where it is actually advantagous to use an array in
> place of a pointer?

They are not the same.

Array:

[0][1][2][3]

Pointer:

[p]->[0][1][2][3]

As others have pointed out, you don't want to use pointers for everything.

Bart.



Wed, 19 May 2004 04:29:29 GMT  
 array vs pointer

Quote:
> 1) Know the amount of memory it takes at compile/link time.

You can also do this with pointers, just put constants instead inside
malloc.

Quote:
> 2) If it is an automatic variable, it is allocated and deallocated
>    automatically.

Good point. But this is not something that can't be done manually (with
malloc and free).

Quote:
> 3) You can take the sizeof an array

True, there is no way to do this with pointers that I know of. But I dont
see this as too much of an advantage, because as you pointed out in (1), the
size of an array is always known at compile time, so that means when you are
writing your code you know how big the array is anyway, so you can just put
the size in the code yourself instead of using sizeof.

Quote:
> 4) You cannot change the addres of an array (this can be a plus)

This was the main difference I saw. Whether or not it is a plus depends on
the programmer, obviously.


Wed, 19 May 2004 09:08:26 GMT  
 array vs pointer

Quote:
> You can't input a line of text into a pointer. Are you planning to
malloc()
> /all/ string areas?

If you mean you cant assign the address of a string to a pointer, you are
incorrect.

char *p;
p = "programming is fun";

is perfectly legitimate because the string constant "programming is fun" is
stored in some memory location to begin with, and setting the pointer to
point to that location will therefore work fine.

Quote:
> How would you convert a month number into a month name or a
> day-of-the-week number into a day name?

the following code seems to work:

 char **month; /*pointer to pointer to char*/
 month = malloc(52); /*gives me 13 pointers, indexes 0-12*/
 month[1] = "January";
 month[2] = "February";
 month[3] = "March";
 month[4] = "April";
...etc...

I agree that a char month[13][10] is easier though, because when you can
initialize all the elements together using braces {} and hence not requiring
12 lines.

i.e. char month[13][10] = {"","January","February","March",...etc...};

But the pointer method has the advantage that later, if you want to change
all the month names to Spanish, you can directly assign without using strcpy
or anything. i.e. month[1] = "Enero"; etc.

On a semi-related note, that brings me to a question, if anyone knows the
answer feel free to chime in... If I allocate, say, 8 bytes of memory, for
example:

char *p;
p = malloc(8);

How can I define multiple dimensions for it, so I can use the memory as
p[2][4] or something instead of just p[8]? thanks

mushu



Wed, 19 May 2004 09:28:49 GMT  
 array vs pointer

Quote:

>> You can't input a line of text into a pointer. Are you planning to
>malloc()
>> /all/ string areas?

>If you mean you cant assign the address of a string to a pointer, you are
>incorrect.

Thats not what was said and taken out of context. The previous poster
(and by the way its polite to leave in the attribution line) said that
you can't input a line of text into pointer, the context being using
scanf to read a line from input.

You can of course assign a value to a pointer, by pointing it to some
allocated memory.

Quote:
>char *p;
>p = "programming is fun";

which is what this does.

Quote:
>> How would you convert a month number into a month name or a
>> day-of-the-week number into a day name?

>the following code seems to work:

> char **month; /*pointer to pointer to char*/
> month = malloc(52); /*gives me 13 pointers, indexes 0-12*/

only if a pointer is 4 bytes, and aligned the same as a pointer to a
pointer.  On some systems this would give you 6.5 pointers, or none at
all....

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>



Wed, 19 May 2004 09:35:16 GMT  
 array vs pointer

Quote:

> > You can't input a line of text into a pointer. Are you planning to
> malloc()
> > /all/ string areas?

> If you mean you cant assign the address of a string to a pointer, you are
> incorrect.

You were complaining that you couldn't see a use for arrays.
Strings are arrays of characters, so you've just contradicted yourself.

Quote:
> char *p;
> p = "programming is fun";

> is perfectly legitimate because the string constant "programming is fun" is
> stored in some memory location to begin with, and setting the pointer to
> point to that location will therefore work fine.

Morris certainly knows that already.

Quote:
> > How would you convert a month number into a month name or a
> > day-of-the-week number into a day name?

> the following code seems to work:

>  char **month; /*pointer to pointer to char*/
>  month = malloc(52); /*gives me 13 pointers, indexes 0-12*/

That's a terrible, awful, unforgiveably bad way to allocate
memory.  Write it as `month = malloc (sizeof *month * 13);' in
the future, please.

Quote:
>  month[1] = "January";
>  month[2] = "February";
>  month[3] = "March";
>  month[4] = "April";
> ...etc...

Why do you insist on wasting the 0th element?  You must not be
thinking in C yet.

Quote:
> I agree that a char month[13][10] is easier though, because when you can
> initialize all the elements together using braces {} and hence not requiring
> 12 lines.

> i.e. char month[13][10] = {"","January","February","March",...etc...};

You may find arrays of pointers even better:
        char *month[13] = {"", "January", ...};

Quote:
> On a semi-related note, that brings me to a question, if anyone knows the
> answer feel free to chime in... If I allocate, say, 8 bytes of memory, for
> example:

> char *p;
> p = malloc(8);

> How can I define multiple dimensions for it, so I can use the memory as
> p[2][4] or something instead of just p[8]? thanks

Read the C FAQ.


Wed, 19 May 2004 09:40:09 GMT  
 array vs pointer

Quote:

> the following code seems to work:

>  char **month; /*pointer to pointer to char*/
>  month = malloc(52); /*gives me 13 pointers, indexes 0-12*/
>  month[1] = "January";
>  month[2] = "February";
>  month[3] = "March";
>  month[4] = "April";
> ...etc...

> I agree that a char month[13][10] is easier though, because when you can
> initialize all the elements together using braces {} and hence not requiring
> 12 lines.

> i.e. char month[13][10] = {"","January","February","March",...etc...};

> But the pointer method has the advantage that later, if you want to change
> all the month names to Spanish, you can directly assign without using strcpy
> or anything. i.e. month[1] = "Enero"; etc.

I have no idea what your above rant is about. The following is perfectly legal:

char * month[12] = { "January", "February", "March", /* ... */ };

Quote:
> On a semi-related note, that brings me to a question, if anyone knows the
> answer feel free to chime in... If I allocate, say, 8 bytes of memory, for
> example:

> char *p;
> p = malloc(8);

> How can I define multiple dimensions for it, so I can use the memory as
> p[2][4] or something instead of just p[8]? thanks

You could try doing that with various casts, but I don't think it's a good idea
anyway. What are you *really* trying to do?

Bart.



Wed, 19 May 2004 10:38:07 GMT  
 array vs pointer

Quote:
> I have no idea what your above rant is about. The following is perfectly
legal:

> char * month[12] = { "January", "February", "March", /* ... */ };

I was saying that if you use char month[12][10] (pure array method) to store
the months, then you can initialize it more easily than with char **month
(pure pointer method) and malloc because you can initialize all 12 months in
one line with {}. So that was an advantage of using arrays. But this
advantage is only available right when you declare the array, later on if
you wanted to change the names of those months you would have to use strcpy,
and with the pure pointer method you would not. Your method, a hybrid of
array and pointer, is actually the best of the three, and one that I
overlooked.

Quote:
> You could try doing that with various casts, but I don't think it's a good
idea
> anyway. What are you *really* trying to do?

I want to know if there is a way to be able to simulate a multdimensional
array (like for a chess board [8][8] is more convenient than [64]) using
pointers and malloc. Really for no purpose other than curiousity.

mushu



Wed, 19 May 2004 11:16:23 GMT  
 array vs pointer

Quote:
> You were complaining that you couldn't see a use for arrays.
> Strings are arrays of characters, so you've just contradicted yourself.

I don't believe a string has to be an array (maybe you are thinking of an
array as something different than I am). Would you consider this an array?

char *p;
p = malloc(10);
p[0] = 'h';
p[1] = 'i';
p[2] = '\0';
printf(p);



Wed, 19 May 2004 11:27:38 GMT  
 array vs pointer

Quote:

> > You could try doing that with various casts, but I don't think it's a good
> idea
> > anyway. What are you *really* trying to do?

> I want to know if there is a way to be able to simulate a multdimensional
> array (like for a chess board [8][8] is more convenient than [64]) using
> pointers and malloc. Really for no purpose other than curiousity.

int ** array, * temp;
array = malloc(rows * sizeof *array);
temp = malloc(rows * cols * sizeof **array);
if(array && temp)
{
    for(i=0; i<rows; i++)
        array[i] = temp + i*cols;

Quote:
}

Bart.


Wed, 19 May 2004 12:16:59 GMT  
 
 [ 23 post ]  Go to page: [1] [2]

 Relevant Pages 

1. pointer to array vs pointer

2. array vs pointer

3. Arrays Vs Pointers

4. array Vs pointer

5. Array vs Pointer for a DataBase(?)

6. Lists VS. Dynamic Arrays VS. Re-defining Arrays

7. Dereferencing f-pointers, arrays of f-pointers, pointers to f-pointers

8. Pointer vs array compile-time distiction

9. Pointer vs array compile-time distinction (macros)

10. pointer vs. array

11. Case-switch vs. Array of function pointers

12. Array of pointers, pointer to array...

 

 
Powered by phpBB® Forum Software