Thread: question about holding a structure in a char array

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    167

    question about holding a structure in a char array

    Code:
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    
    typedef unsigned char byte;
    
    typedef struct complex
    {
            int a,b;
    } complex;
    
    
    int main()
    {
            complex z,t;
            byte a[1024];
    
            z.a=1;
            z.b=4;
    
    
            memcpy(a,&z,sizeof(z));
            memcpy(&t,a,sizeof(struct complex));
    
            printf("&#37;d %d\n",t.a,t.b);
    
            return 0;
    }
    this works just fine I guess. I was wondering if I can do something like index a part from that array like a structure. something like:

    Code:
    ((struct complex)a).a=1;
    ((struct complex)a).b=4;
    or maybe something like this:

    Code:
            complex z,t;
            void *p;
    
            z.a=1;
            z.b=4;
            p=&z;
    
            t.a=(complex*)p->a;
            t.b=(complex*)p->b;
    the last 2 code snippets don't work are just a hint of what I'm trying to do!
    Last edited by spank; 09-04-2007 at 06:26 AM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What are you ACTUALLY trying to do? It would probably be better to have a bigger picture of what you're trying to achieve, as that will give you better advice. [A bit like, "How do I loose a wheelnut", when you want to fix a puncture - you'd just come back with, how do I attach the jack to lift the car now, and so on - instead of saying "I've got a puncuture, how do I replace my wheel?"]

    --
    Mats

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    167
    thanks for the advice matsp. the thing is that if I can find a way to address a member of a structure using a void pointer or something else would be a easy and clean aproach for me. The first code snippet is a way I can deal with it... I was looking for a more direct aproach.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, I still don't quite understand what problem you are trying to solve. If you want to, you could write your own malloc function that uses a array of char, like this:

    Code:
    #define SOMEBIGNUMBER 100000
    void *mymalloc(size_t size)
    {
       static char  bigarray[SOMEBIGNUMBER];
       static char *ptr = bigarray;
    
       void *res = NULL;
    
       if (size + ptr < &bigarray[SOMEBIGNUMBER]) {
          res = ptr;
          ptr += size;
       }
       return res;
    }
    This of course doesn't allow for data being freed, but it shows you how you can store some data in an array in a very flexible way - I just don't quite understand WHY you would want to do that.

    --
    Mats

  5. #5
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    Switch from this
    Code:
    t.a = (complex*)p->a;
    t.b = (complex*)p->b;
    to this
    Code:
    t.a = ((complex*)p)->a;
    t.b = ((complex*)p)->b;
    and it should work fine.

    "->" operator has a highest preseance (duh, is this the right word?) than the cast operator.

  6. #6
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    Code:
    typedef struct
    {
            int a,b;
    } myStruct;
    
    int main()
    {
    	char test[sizeof(myStruct)];
    	(*(myStruct*)test).a = 1;
    	return 1;
    }

  7. #7
    Registered User
    Join Date
    Dec 2005
    Posts
    167
    ( you're right foxman. stupid me for not realizing that.

  8. #8
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    [QUOTE=foxman;668584]preseance (duh, is this the right word?) QUOTE]

    You mean "precedence"

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  9. #9
    Registered User
    Join Date
    Dec 2005
    Posts
    167
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    typedef struct complex
    {
            int a,b;
    } complex;
    
    typedef struct complex2
    {
            double f,g;
    } complex2;
    
    int main()
    {
            complex a;
            complex2 b;
            void *p[20];
    
            p[0]=calloc(1,sizeof(a));
            p[1]=calloc(1,sizeof(b));
    
            ((complex*)p[0])->a=1;
            ((complex*)p[0])->b=2;
    
            ((complex2*)p[1])->f=3;
            ((complex2*)p[1])->g=4;
    
    
            printf("&#37;d+%di\t%lf+%lfi\n",((complex*)p[0])->a,((complex*)p[0])->b,((complex2*)p[1])->f,((complex2*)p[1])->g);
    
            return 0;
    }
    something like this I was trying to accomplish. I just post it for someone who would be interested in it in the future.

    Thank you for your time!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. simple char array question
    By yahn in forum C++ Programming
    Replies: 4
    Last Post: 02-08-2006, 09:18 PM
  2. linked list inside array of structs- Syntax question
    By rasmith1955 in forum C Programming
    Replies: 14
    Last Post: 02-28-2005, 05:16 PM
  3. linked lists, strcmp(), char array, & a structure
    By xddxogm3 in forum C++ Programming
    Replies: 5
    Last Post: 10-04-2003, 06:24 PM
  4. 2 dimensional char array question
    By knutso in forum C++ Programming
    Replies: 4
    Last Post: 12-05-2002, 06:28 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM