Benny
Benny

Reputation: 35

Is there a way to have a 2D array in C, which has both integers and characters?

I am writing a code which asks for a 2D array, and in that array there are names of toys and their costs. So I was wondering if there is a way that that was possible, because I tried it and it didn't really work.

Upvotes: 0

Views: 212

Answers (5)

John Bollinger
John Bollinger

Reputation: 181149

Every C array has exactly one element type, but that type can be a structure or union type, or a pointer type that points to an object of one of those kinds. For example,

union int_or_string {
    int as_int;
    char *as_string;
};

union int_or_string[5][5];

But I wonder whether what you really want is an one-dimensional array of structures:

struct toy {
    char *name;
    int cost;
};

struct toy array[42];

Upvotes: 2

My understanding of your question (its title) is that you want an array of things which are either names or numbers

You conceptually want a sum type, also known as a tagged union. You should reason with abstract data types in mind (so document first all the operations of your ADT). SICP is a freely available book explaining that notion (but it does not use C) and is a good introduction to programming in general.

C don't have sum types natively but provide you with raw union types, which can be used to build a sum type. See this example.

Perhaps you simply want an array of things having both names and numbers.... Then:

If you just want a product type (i.e. array of things which have both a name and a cost), use a struct

But arrays in C are made of components all having the same type. They are always mono-dimensional, but you could fake 2D arrays with arrays of arrays. In particular, matrixes (of varying dimension) don't exist in C, but you can quite easily mimic them (e.g. by building your own abstract data type for them).

Pointers are also very important in C. I won't dare explaining here why. You need to read some good C programming book (explaining C dynamic heap allocation and why and when arrays decay into pointers). The notions of virtual address space and of pointer aliasing are practically important.

Perhaps you don't need a huge array of mostly empty entries. In some cases, you should consider more complex data structures. Read some Introduction to Algorithms. Maybe you need some hash table. C doesn't provide these natively, but gives you enough basic building blocks to implement them (in your library).

My feeling is that you don't really need 2D arrays, but I don't know your overall problem and I could be wrong.

Consider also studying the source code some existing free software programs (e.g. on github or elsewhere). They could inspire you (in particular since coding conventions are very important in practice).

BTW, the C11 specification is downloadable as n1570 (but it is not an introduction to C programming).

Don't forget to compile with all warnings and debug info, with GCC that is gcc -Wall -Wextra -g. Improve your code to get no warnings. Then use the debugger gdb.

Upvotes: 1

user2736738
user2736738

Reputation: 30926

Towards the solution...

Well this is why there is a thing called structure. You put the name and their price-s in a structure and then all those structure variables are put in an array. (Here a simple demonstration is made which didn't use a 2d array rather 1d array of structs is used).

Example (Illustration only)

struct toy{
   char name[100];
   double price;
}

struct toy toyArr[50];

And then you will do something like (to get input)

for(size_t i = 0; i < 50; i++){
   if( scanf("%lf",&toyArr[i].price) != 1){
     fprintf(stderr,"Error in input");
     exit(1);
   }
   if( scanf("%99s",toyArr[i].name) != 1){
     fprintf(stderr,"Error in input");
     exit(1);
   }
   ...
}

Arrays are too strict...

Also as you were having confusion about array type §6.2.5

An array type describes a contiguously allocated nonempty set of objects with a particular member object type, called the element type.1 The element type shall be complete whenever the array type is specified. Array types are characterized by their element type and by the number of elements in the array. An array type is said to be derived from its element type, and if its element type is T , the array type is sometimes called ''array of T ''. The construction of an array type from an element type is called ''array type derivation''.

1Emphasis mine

Way of using array (on the light of your question).

That should be a particular member object type, not a mixture of types or anything like what you were thinking.

We can get ahead of this stringent type similarity by using constructs like structure etc, which provide us with exactly what you want. Then also the array is of similar struct type but as the struct can contain different types inside of it, it provides us with what you need.

Upvotes: 1

haccks
haccks

Reputation: 106092

Arrays are data structures that contains data of same object type. So the answer to your question is No.
You can rather use structures for this purpose.

struct Data{
    char *str;
    int n;
};

struct Data data[SIZE] // Array of struct of SIZE struct Data

Upvotes: 1

Sergi Pasoevi
Sergi Pasoevi

Reputation: 2851

I am not sure if your code asks for 2d arrays. I imagine an array of structs more logical:

struct toy {
    char *name;
    double price;
}

struct toy toys[NUMBER];

Upvotes: 1

Related Questions