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.
Trying to make a header for Huffman Coding, and running into the following problem:
I'm trying to initialize a struct member, which is an int array, and am not clear how to parse this. The following code is what I'm working with.
Code:
#include <stdio.h>
#include <stdlib.h>
#define MAGIC 0x46465548
// Header for a Huffman-coded file.
typedef struct
{
// magic number
int magic;
// symbols' frequencies
int frequencies[SYMBOLS];
// checksum for frequencies
int checksum;
}Huffeader;
int main(void)
{
int totalcount = 0;
int chr;
// open text file
FILE* fp;
fp = fopen("hth.txt","r");
if (fp == NULL)
{
printf("could not open file to read\n");
return 1;
}
// declare array for character count
int charcnt[256];
// initialize all values = 0
for( int i = 0; i < 256; i++)
{
charcnt[i] = 0;
}
// tally count of each character in text
while ((chr = fgetc(fp)) != EOF)
{
charcnt[chr]++;
// and get total count of characters
totcount++;
}
// declare a Huffeader
Huffeader header;
// initialize members
header.magic = MAGIC;
header.frequencies[255] = PROBLEM HERE
header.checksum = totcount;
free(fp);
return 0;
}
Don't know how to go about initializing the above member "header.frequencies[255]. Do I need memcpy()? If so, how to parse?
Last edited by atlantis43; 02-14-2015 at 11:51 AM.
#include <stdio.h>
#include <stdlib.h>
#define MAGIC 0x46465548
//#define SYMBOLS 256
// Header for a Huffman-coded file.
typedef struct
{
// magic number
int magic;
// symbols' frequencies
int frequencies[SYMBOLS];
// checksum for frequencies
int checksum;
}Huffeader;
int main(void)
{
int totcount = 0;
int chr;
// open text file
FILE* fp;
fp = fopen("hth.txt","r");
if (fp == NULL)
{
printf("could not open file to read\n");
return 1;
}
// declare array for character count and initialize
int charcnt[256] = {};
// tally count of each character in text
while ((chr = fgetc(fp)) != EOF)
{
charcnt[chr]++;
// and get total count of characters
totcount++;
}
// declare a Huffeader, initialize members
Huffeader header = {MAGIC,{},totcount};
free(fp);
return 0;
}
It's an array of integers so if you want to set the 256 slot in the array to 0 just do so. If you want to initialize the entire array you have various options.
Code:
struct Heffeader header = {0};
Will initialize the entire struct. You can then individually set the members. You can also do:
It's an array of integers so if you want to set the 256 slot in the array to 0 just do so. If you want to initialize the entire array you have various options.
Code:
struct Heffeader header = {0};
Will initialize the entire struct. You can then individually set the members. You can also do:
If you have a block of memory you wish to assign to frequencies you could use memcpy or iterate over them assign individual values.
Code:
for (int i = 0; i < SYMBOLS; ++i) {
header.frequencies[i] = symbols[i];
}
Thanks to both above replies, I now realize that I neglected to initialize all frequencies[] to 0. What I should have asked was "How to set the values of the frequecies array".
The "for loop" looks quite simple and adequate, but what would be the parsing for the use of memcpy()?
What I meant was how to parse source and dest, but figured it out without a problem:
memcpy(header.frequencies,charcnt,256 );
Thanks to all.
That probably isn't what you want. It will copy 256 bytes to header.frequencies from charcnt but the size of header.frequencies is going to be
[code]
SYMBOLS * sizeof (int);
[code]
so if SYMBOLS == 256 you will have copied into the first 256 / sizeof (int) members of the array.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.