Wrox Programmer Forums
|
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old November 12th, 2005, 06:55 PM
Authorized User
 
Join Date: Jan 2005
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default declaring structs

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 With Quote
  #2 (permalink)  
Old November 13th, 2005, 06:08 AM
Authorized User
 
Join Date: Nov 2005
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to Paramesh
Default

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 With Quote
  #3 (permalink)  
Old November 19th, 2005, 11:24 AM
Authorized User
 
Join Date: Jan 2005
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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 With Quote
  #4 (permalink)  
Old April 6th, 2006, 12:44 AM
Authorized User
 
Join Date: Oct 2004
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to C@uark
Default

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
Reply With Quote





Similar Threads
Thread Thread Starter Forum Replies Last Post
Enums VS Structs MoDulus Intro Programming 4 January 19th, 2012 10:42 AM
arrays of structs seymour_glass C# 2 April 2nd, 2007 08:14 PM
SizeOf Structs CDMBR C# 1 February 21st, 2007 06:24 PM
Declaring a dropdownlist Rod Merritt BOOK: ASP.NET Website Programming Problem-Design-Solution 0 August 1st, 2006 02:11 PM
structs - read in from data. mclavo Visual C++ 0 October 25th, 2004 10:49 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.