JustKernel

Ray Of Hope

Basic C – Keep learning..

Today I faced a rather simple problem for which , though I was sure of finding a solution, I couldn’t find a solution.

I had a struct organized as

struct

{

unsigned short msg_id;

char *data;

}tmp;

struct tmp st_data;
unsigned short var;

Now I needed to extract the first two bytes from the structure variable, and store it in a seperate variable var..

Though I could have done var = st_data->msg_id, but I wanted to do it by extracting the first two bytes from the structure memory and save it in var variable. But I couldn’t 🙁

The approaches I tried

var = 8(uint16*)(void*)&st_data;

var = *st_data;

var = (var >> 8) | *(st_data + 1);

and some other weired approaches. But couldn’t succeed.. So still trying….. 🙂

Another important point I noticed today

typdef struct

{

int abc;

st_data *next;

}st_data;

//error at the point of declaration of next, st_data is not defined.

Correction

typedef struct st_data

{

int a;

struct st_data *next;

}st_data_t;

Noticed by a fresher working under me.. There is no end to learning.. Good 🙂

Anshul Makkar, anshul_makkar@justkernel.com

Originally Posted on: 2010-04-16 04:28:23


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.