LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-15-2012, 02:35 AM   #1
chinabenjamin66
Member
 
Registered: Mar 2012
Location: Shenzhen Chian
Posts: 73

Rep: Reputation: Disabled
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.
 
Old 12-15-2012, 02:49 AM   #2
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
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.
 
Old 12-15-2012, 03:00 AM   #3
chinabenjamin66
Member
 
Registered: Mar 2012
Location: Shenzhen Chian
Posts: 73

Original Poster
Rep: Reputation: Disabled
Smile

Quote:
Originally Posted by millgates View Post
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.
 
Old 12-15-2012, 04:00 AM   #4
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
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);
 
Old 12-16-2012, 08:34 AM   #5
chinabenjamin66
Member
 
Registered: Mar 2012
Location: Shenzhen Chian
Posts: 73

Original Poster
Rep: Reputation: Disabled
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'?
 
Old 12-16-2012, 10:11 AM   #6
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
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.
 
Old 12-16-2012, 06:48 PM   #7
chinabenjamin66
Member
 
Registered: Mar 2012
Location: Shenzhen Chian
Posts: 73

Original Poster
Rep: Reputation: Disabled
Thank you very much, it is very helpful!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Convert length-indicated variable length record file to LF-terminated Z038 Linux - General 10 11-29-2012 11:59 PM
Extracting nth length of character from a variable to another variable hayloiuy Linux - Newbie 4 05-02-2011 03:47 PM
How to eliminate "ISO C++ forbids variable length array" warning? chinho Programming 6 01-28-2011 03:54 AM
How to define hex character array in C? MrUmunhum Programming 9 09-08-2008 07:16 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:21 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration