Subject: declaring structs
Posted By: scoobie Post Date: 11/12/2005 5:55:51 PM
Hi,

i'm new to c programming. i have declared a typedef struct that contains two arrays in it. the problem is when i try to reference something that is in the struct i get an error. here is the code

#include <stdio.h>
#include <stdlib.h>

typedef struct
{
unsigned short key1[2];
unsigned short plain[2];
}code;

unsigned short *res_k1;
unsigned short *res;


main(int argc, char *argv[])

{
unsigned short k1[2],k2[2];

unsigned short p[2],c[2];
res_k1 = (unsigned short *) malloc(sizeof (code));
        

k1[0]=0x0000;

k1[1]=0x0000;


short i;


p[0]=0x0001;

p[1]=0x0002;
        
for (i=0; k1[0] < 0x00FF; i++)        
{
for (i=0; k1[1] < 0xFFFF; i++)

{

encode(p, k1);
printf("  Result  =  ");

printf("(%4x,%4x)    ",p[0],p[1]);

}
                
encode(p, k1);
printf("  Result  =  ");

printf("(%4x,%4x)    ",p[0],p[1]);    

}

} //end main

also i declare an array unsigned short[17000000]; does anybody know i pass in the results of p to that array;

thanks in advanced for all you help,

sc
Reply By: Paramesh Reply Date: 11/13/2005 5:08:05 AM
Please be more specific.
Post the error message you get.


"Don't walk behind me; I may not lead.
 Don't walk in front of me; I may not follow.
 Just walk beside me and be my friend."
Reply By: scoobie Reply Date: 11/19/2005 10:24:47 AM
hi,

i managed to get my struct to work.the problem i am having now is i am trying to use calloc memory allocation but i keep getting a segmentation fault and i don't know how to fix it. i don't think i am declaring the calloc properly and storing the result of encode properly.

here is my code that i am using.

#include <stdio.h>
#include <stdlib.h>
typedef struct code1
{
    unsigned short key[2];

    unsigned short text[2];
}code;


    void encode(short* v, short* k)

    { ... }


    void decode(short* v,short* k)  

    { ... }


    main(int argc, char *argv[])

    {        

        unsigned short key1[2],key2[2];

        unsigned short p[2],c[2];

    
        struct code1 *test, *entry;

        entry = (unsigned short *) calloc(2, sizeof (code));

        key1[0]=0x0000;

        key1[1]=0x0000;    

        short i;

        p[0]=0x0001;

        p[1]=0x0002;


        for (i=0; key1[0] < 0x0002; i++)
        {
            for (i=0; key1[1] < 0x000A; i++)

            {

                encode(p, key1);

                test[i].key[0] = key1[0];
                test[i].key[1] = key1[1];
                test[i].text[0] = p[0];
                test[i].text[1] = p[1];

            }

        }

} //end main

can anybody help?

thanks,

sc

Reply By: C@uark Reply Date: 4/6/2006 12:44:25 AM
change:
entry = (unsigned short *) calloc(2, sizeof (code));

you have declared "entry" as a pntr to a code1 structure so when you call calloc type cast it to the appropriate type.
entry = ( struct code1* ) calloc(2, sizeof (code)); // >>first arg is the number of elements in an array you   
                                                    //wish to allocate.<<
                                                    // >>second arg is the size of each element, in
                                                   // in this case the size of a code1/code structure.<<
calloc always returns a void pntr or NULL if insufficient memory, you must type cast it to the type of the receiving variable in this case a struct code1*.
Now since you have dynamically allocated the memory for "entry" you would also dereference the structure member variables by using the indirect access operator -> i.e.  entry[i]->key[0] = key1[0]; // i being the subscript into the array of structs that you have allocated

Go to topic 42429

Return to index page 322
Return to index page 321
Return to index page 320
Return to index page 319
Return to index page 318
Return to index page 317
Return to index page 316
Return to index page 315
Return to index page 314
Return to index page 313