Two-dimensional Matrix in C

Asked

Viewed 131 times

0

I have a record of products in C. Products can have up to 30 characters (columns), but can be infinite in quantity (rows).

In the variable declaration, I used:

char products[][30];

but when compiling, it returns that the size of the array cannot be empty. So if I put a number of lines,

char products[10][30];

I am statically pre-defining the amount of products.

How to leave this number free so that the matrix auto-increments as the user enters the data?

  • 1

    It seems to me that you will have to use a dynamic list.

  • 1

    Search by dynamic memory allocation, malloc.

  • 1

    If you can know the size at some point you can do as the first duplicate, otherwise you have to do as in the last ones, which you need to be very careful, it is very easy to do wrong. It’s the subject most asked in C here because almost nobody can do it right, IE, everyone will have enough difficulty in being real programmers.

  • @Maniero yes, so it is necessary to seek the correct way of doing, indeed NO one is born knowing ;)

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.