LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 02-14-2015, 11:48 AM   #1
atlantis43
Member
 
Registered: Feb 2013
Posts: 289

Rep: Reputation: Disabled
C struct member: array initialization


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.
 
Old 02-14-2015, 12:08 PM   #2
metaschima
Senior Member
 
Registered: Dec 2013
Distribution: Slackware
Posts: 1,982

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Try:
Code:
#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;
}
 
Old 02-14-2015, 12:16 PM   #3
SoftSprocket
Member
 
Registered: Nov 2014
Posts: 399

Rep: Reputation: Disabled
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:

Code:
struct Heffeader header = { MAGIC, { 0 }, totcount };
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];
}
 
1 members found this post helpful.
Old 02-14-2015, 12:47 PM   #4
atlantis43
Member
 
Registered: Feb 2013
Posts: 289

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by SoftSprocket View Post
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:

Code:
struct Heffeader header = { MAGIC, { 0 }, totcount };
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()?
 
Old 02-14-2015, 01:13 PM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
What do you mean by 'parsing memcpy'?
 
Old 02-14-2015, 01:21 PM   #6
atlantis43
Member
 
Registered: Feb 2013
Posts: 289

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by NevemTeve View Post
What do you mean by 'parsing 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.
 
Old 02-14-2015, 01:53 PM   #7
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Another idea would be forget 'charcnt' and use header.frequencies for the counting.
 
Old 02-14-2015, 02:39 PM   #8
SoftSprocket
Member
 
Registered: Nov 2014
Posts: 399

Rep: Reputation: Disabled
Quote:
Originally Posted by atlantis43 View Post
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.
 
  


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
array of struct ChimpFace9000 Programming 7 12-20-2017 11:46 PM
gcc differences with C-struct initialization ta0kira Programming 2 08-09-2008 12:55 PM
GCC compile problem:struct A have a member variable which is just a struct type name? leon.zcom Programming 3 04-18-2008 04:40 PM
g++ doesn't support "C-struct member initialization" extension of gcc? ta0kira Programming 8 04-13-2008 02:57 PM
g++ and wrong struct member addresses / struct size misreporting sonajiso Linux - General 5 05-22-2004 10:16 PM

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

All times are GMT -5. The time now is 10:25 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