LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to define a variable-length array which can holds hex num? (https://www.linuxquestions.org/questions/programming-9/how-to-define-a-variable-length-array-which-can-holds-hex-num-4175441531/)

chinabenjamin66 12-15-2012 02:35 AM

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.

millgates 12-15-2012 02:49 AM

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.

chinabenjamin66 12-15-2012 03:00 AM

Quote:

Originally Posted by millgates (Post 4849722)
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.

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.

Did I clearly describe?  I hope so.

millgates 12-15-2012 04:00 AM

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:

Code:

int i = 0x4f;
printf("\\x%x\n", i);


chinabenjamin66 12-16-2012 08:34 AM

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'?

millgates 12-16-2012 10:11 AM

Quote:

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.

Example:
Code:

#define CONTAINER_INIT_SIZE 8

typedef unsigned member_t;

typedef struct {
    member_t *items;
    size_t size;
    size_t capacity;
} container;

You would have to initialize the container before first use (equivalent of a C++ constructor):
Code:

void containerInit(container *c) {
    c->size = 0;
    c->capacity = CONTAINER_INIT_SIZE;
    c->items = (member_t*)malloc(sizeof(member_t) * CONTAINER_INIT_SIZE);
}

Then you can add numbers to it, resize it when necessary:
Code:

void containerPush(container *c, member_t v) {
    c->size ++;
    if (c->size > c->capacity) {
        c->capacity *= 2;
        c->items = (member_t*)realloc(c->items, sizeof(member_t) * c->capacity);
    }
    c->items[c->size - 1] = v;
}

You should also clean the container when you don't need it anymore. Also, you should make sure that the (re)allocations are successful.

Quote:

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'?
The question should be how to read something like '\x93','\x76'. The answer would then be use the format string of scanf as required.

Code:

scanf(" \\x%x", &i);
You will have to make sure your code can handle input that is not in the expected format, but I'll leave that to you.

chinabenjamin66 12-16-2012 06:48 PM

Thank you very much, it is very helpful!


All times are GMT -5. The time now is 02:23 PM.