stewazy
stewazy

Reputation: 139

Pointer to an array type (custom types)

I have a custom array type defined as in the code and a pointer to that array type also defined. I'm trying to find a correlation between the custom array type and a pointer to the custom array type. How do I assign a value to the pointer of the custom array type so I can iterate through the array using the pointer?

#include<stdio.h>

typedef int array_type[3];
typedef array_type* ptr_array_type;

int main() {
   array_type a = {2,3,4};
   ptr_array_type ptr;

   ptr = a;

   printf("%d\n",*ptr);

}

I know the array name contains the pointer to the first element. I keep getting an invalid printout: -50204788 instead of 2.

I also get a warning:

warning: assignment to 'ptr_array_type' {aka 'int (*)[3]'} from incompatible pointer type 'int *' [-Wincompatible-pointer-types] ptr = a;

Could someone point me in the right way to assign a proper value to the pointer so I can iterate through the array.

Upvotes: 1

Views: 1175

Answers (3)

John Bode
John Bode

Reputation: 123596

ptr_array_type aliases int (*)[3], not int * - you will not be able to use it to iterate through array_type. You could use it to iterate through an array of array_type, like so:

array_type arr[3];
ptr_array_type ptr = arr;

while ( size_t i = 0; i < 3; i++ )
  do_something_with( ptr[i] );

If you want to be able to iterate through an array of T with a pointer, then the type of that pointer must be T *. So you'd need to change your typedefs to something like

typedef int array_type[3];    // probably want to replace int with another
typedef int *ptr_array_type;  // typedef to abstract out the type
...
array_type a = {1, 2, 3}; // yuck! don't do this! a is not obviously an array,
                          // so an array initializer looks wrong in this context
ptr_my_type p = a;

printf( "%d", *p );

However...

If you're using typedef to abstract away implementation details for a type, then you should abstract away all implementation details for that type, including things like dereferencing and subscripting. You'll want to create a full API, such as:

 array_type *arr = new_array_type( 1, 2, 3 );  // even this is leaky since it assumes we know
                                               // the array element type is integral
 array_type_iterator *itr = new_array_type_iterator( arr );
 array_element_type target;

 begin_iterator( &itr ); 
 while( next_iterator( &itr ) )
 {
   get_data_from_iterator( itr, &target );
   write_array_element_to( stdout, target );
 }

 free_array_type_iterator( itr );
 free_array_type( arr );

Partial or leaky abstractions just lead to code that's confusing to use and maintain. If the user of a type has to be aware of that type's "array-ness" or "pointer-ness" in order to use it properly, then don't hide those qualities behind a typedef without providing replacements for the [] and unary * operators as well. Also provide some way to abstract out I/O - don't force the user to have to know whether to use %d or %f or %s in a printf statement if they're not working with a primitive type.

Upvotes: 0

John Bollinger
John Bollinger

Reputation: 181932

How do I assign a value to the pointer of the custom array type so I can iterate through the array using the pointer?

Given array_type defined by

typedef int array_type[3];

, the pointer type to which you can assign an array_type is int *:

typedef int *ptr_array_element;

You can (seemingly) assign an object of array_type to a pointer of type ptr_array_element:

array_type a = {2, 3, 4};
ptr_array_element p = a;

Having done so, you can access the first array element via the derferencing operator.

assert(*p == 2);

And you can generally use it a bit like a C++ iterator:

printf("%d\n", *p++);
printf("%d\n", *p++);
printf("%d\n", *p++);

BUT DON'T.

Use typedefs sparingly, where they hide irrelevant detail, substantially improve code clarity, or similar. Avoid using typedefs that obscure relevant details, such as that objects of that type are pointers or arrays, unless, possibly, those details are abundantly clear from the type names, or unless hiding them is intentional and harmless. It is rarely harmless to hide array or pointer nature.

Upvotes: 2

Govind Parmar
Govind Parmar

Reputation: 21572

ptr is a pointer to int[3]; a is an int[3]. While I don't like typedefing pointer types in general, if you insist on this approach, here's how you can dereference into the first element of a from ptr:

ptr = &a;
printf("%d\n", **ptr);

Upvotes: 1

Related Questions