How to define a variable-length array which can holds hex num?
Hello friends,
How to define a variable-length array which can holds hex number? those hexadecimal number should be stored after type in from stdin. Can anyone give any ideas, I am a beginner of C programming. any idea will be appreicatd. |
Hi,
Can you show us what have you tried? 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. |
Quote:
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. Did I clearly describe? I hope so. |
One possibility would be using realloc.
1) start with allocating a small array. Code:
int *array=(int*)malloc(SIZE*sizeof(int)); 3) if you reach the end of allocated arra, use realloc to increase the size: Code:
array=(int*)realloc(array, NEWSIZE*sizeof(int)); Quote:
Code:
int i = 0x4f; |
Hello millgates,
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'? |
Quote:
Example: Code:
#define CONTAINER_INIT_SIZE 8 Code:
void containerInit(container *c) { Code:
void containerPush(container *c, member_t v) { Quote:
Code:
scanf(" \\x%x", &i); |
Thank you very much, it is very helpful!
|
All times are GMT -5. The time now is 02:23 PM. |