[SOLVED] How to define a variable-length array which can holds hex num?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
What exactly do you mean by "variable length array"? You may use realloc() to change the size of a dynamically allocated array. You can also implement a linked list or a tree. Depends on what you want to do with the data.
Also, an integer is an integer. Whether it is hex, dec or oct or whatever depends on its interpretation, not the way it is stored.
What exactly do you mean by "variable length array"? You may use realloc() to change the size of a dynamically allocated array. You can also implement a linked list or a tree. Depends on what you want to do with the data.
Also, an integer is an integer. Whether it is hex, dec or oct or whatever depends on its interpretation, not the way it is stored.
Sorry for vague description of the original post.
I would like to define a array which its length cannot be fixed, for example, I input 5 numbers, it will hold 5 numbers, input 10 numbers, it wil hold 10 numbers.
As for its memeber,I would like it to store like '\x3a', '\x93', '\xa2', '\x95' etc.
One possibility would be using realloc.
1) start with allocating a small array.
Code:
int *array=(int*)malloc(SIZE*sizeof(int));
2) keep track of how much memory you have allocated and how many numbers are stored in your array.
3) if you reach the end of allocated arra, use realloc to increase the size:
Code:
array=(int*)realloc(array, NEWSIZE*sizeof(int));
Quote:
As for its memeber,I would like it to store like '\x3a', '\x93', '\xa2', '\x95' etc.
The numbers are stored as sequences of ones and zeroes. The difference is only how you read and print them. Unless you want to store them as strings. Example:
I am a beginner of C programming, so I would like you explain how to keep track of how much memory I have allocated and how many numbers are stored in your array.
Further question, I want the array to be assigned by standard input. how to do? if I use scanf, how to make it store something like '\x93','\x76'?
I would like you explain how to keep track of how much memory I have allocated and how many numbers are stored in your array.
Just declare one variable for the allocated space and one for the number of elements already read. It might be practical to put it in a struct to create a container similar to a C++ vector.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.