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.
-
3Create an array of `struct` containing the name and the cost. – Matteo Italia Dec 16 '17 at 13:49
-
So can this be done without a 2D array? – Benny Dec 16 '17 at 13:55
-
• When the program loads, the user can input the list of this year’s presents and the price of each present into a PresentArray • There should be at least 10 different presents, and each present should have a name & price: o e.g. Doll, 10 o Price of each item should be between 5 and 15 – Benny Dec 16 '17 at 13:56
-
Notice that SO is not a *do-my-homework* service, so your question is off-topic.... – Basile Starynkevitch Dec 16 '17 at 14:10
-
To be fair Basile Starynkevitch I didn't really ask for you to do my homework, i jsut asked a question on whether this is possible in C. I mean its not like I asked you guys to code the task for me, i am just trying to get good at programming, and if you still think it's off topic, then i'll just have to figure it out some other way – Benny Dec 16 '17 at 14:20
-
Then your question is probably too broad. But I did take time to collect relevant information for you in my answer. You really should spend a few weeks in reading books, and you should at least follow all the links I have given. BTW, I don't think that you need 2D arrays, but I could be wrong. – Basile Starynkevitch Dec 16 '17 at 14:26
-
1I can imagine someone might write a document, perhaps a page of an on-line toy store, in which there is a table with two columns: names of toys in the left column, and their prices in the right column. It is (almost) never correct to encode a table like that as a 2D array. Who said you need a 2D array, and did they explain why? – David K Dec 16 '17 at 14:38
-
Showing the code that you tried, even though it did not work, would likely give valuable clues about what problem you actually were trying to solve. – David K Dec 16 '17 at 14:41
-
ok, well I'll shpw you the code – Benny Dec 16 '17 at 15:16
-
Well this is just the start and I am justing trying to get this part to work: – Benny Dec 16 '17 at 15:21
-
#include
#include – Benny Dec 16 '17 at 15:22#include int main() { char PresentArray[10][2]={"Airplane, 10, Barbie, 15, Crane, 7, Doll, 12, Engino, 15, Furby, 12, Gauntlet, 13, Hotwheels, 7, Isles, 6, Jack-Inn-The- Box, 10"}; int i,j; for (i=0;i -
Do not post code in a comment. [edit] your question and add it in there. You may want to make sure it's formatted as `code` to avoid further downvotes from irritated persons who are *trying* to help you. – Jongware Dec 16 '17 at 17:24
5 Answers
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];

- 69,737
- 10
- 105
- 255

- 160,171
- 8
- 81
- 157
-
If you don't mind me asking - have you noticed one thing in question *there are names of toys and their costs* .and you suggested union which is good I would say - but to use that OP needs to remember that which block is using which type. Isn't it? Correct me if I am wrong. That is precisely why I was avoiding this `union` suggestion. – user2736738 Dec 16 '17 at 14:22
-
@coderredoc, yes, to use a union, the program does need to know which array elements use which union member. This is not necessarily a big deal. For example, if the OP is using his 2D array to store a table, then the array indices may convey what union member should be used for each. One can also make a union carry that sort of information internally. But none of that was the subject of the question. – John Bollinger Dec 16 '17 at 14:37
-
Yes exactly my thought. Ah no, it's not a big deal TBH. But just I wonder if OP knows that. Good answer btw. Now the thing is that means now the indices will hold some information regarding the type of the data isn't it? Yes even the other solution could be using another array to store that information. Or simply embed in a `struct` that union and use it like that along with that type info ( a seperate variable). But then again we are moving towards `struct` solution. – user2736738 Dec 16 '17 at 14:38
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];

- 2,833
- 3
- 27
- 41
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

- 104,019
- 25
- 176
- 264
-
ok, so what is the data type for struct, i mean hwo whould you write struct, as in the same way you would write an integer as %d, how would struct be written – Benny Dec 16 '17 at 16:08
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.

- 30,591
- 5
- 42
- 56
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
.

- 223,805
- 18
- 296
- 547
-
I even guess that the OP needs to read *several* books (one focused on programming, and another focused on the C programming language). – Basile Starynkevitch Dec 16 '17 at 14:16
-
1yes true..that's needed for sure. But really sometimes these questions turns out to be `X-Y` problem - like here there is 0.1% chance that OP needs 2d array - but the idea of different types in C array made OP want one. – user2736738 Dec 16 '17 at 14:17
-
I believe the OP wants others to do his homework. I don't think that SO is the right site for that. And I also believe that students are learning much more by doing their homework without help from websites. – Basile Starynkevitch Dec 16 '17 at 14:25