Structure element accessing issue...

Hi all,
Can someone advice, why I'm getting a segmentation fault on a Linux system for a program which looks like the below one--:

typedef struct idev{
int p,
char q[10];
} Idev;

typedef struct abc{
int i;
Idev *idev_ptr;
} ABC;

int main(){
ABC a_var;

char str[10];
int i_var = 0;

/* I received 'a_var' populated by some function call as follows---*/

int result = foo(&a_var);
/Received result == SUCESS/

/initializing str[] to all zero./
bzero(str, sizeof(str));

if(a_var.idev_ptr != NULL)
{
/Access to this is pretty fine/
i_var = a_var.idev_ptr->p;

/But accessing array element gives the segmentation fault/
strncpy(str, a_var.idev_ptr->q, 10);
}

if(a_var.id_q != NULL)
{
free(a_var.i_d); /freed the memory/
}

return 0; /end of the program/
}

Now please advice me, why I get the segmentation fault by just accessing a_var.idev_ptr->q ???

typedef struct idev
{
	int p,
	char q[10];
} Idev;

typedef struct abc
{
	int i;
	Idev *idev_ptr;
} ABC;


int main()
{
	ABC a_var={0,NULL};
	
	char str[10]={0x0};
	int i_var = 0;
	
	/* I received 'a_var' populated by some function call as follows---*/
	
	int result = foo(&a_var);
	/*Received result == SUCESS*/
	
	/*initializing str[] to all zero.*/
	bzero(str, sizeof(str)); 
	
	if(a_var.idev_ptr != NULL)
	{
	/*Access to this is pretty fine*/
		i_var = a_var.idev_ptr->p;
		
		/*But accessing array element gives the segmentation fault*/
		strncpy(str, a_var.idev_ptr->q, 10);
	}


	if(a_var.id_q != NULL)
	{
	free(a_var.i_d); /*freed the memory*/
	}
	
	return 0; /*end of the program*/
}

foo() has to call malloc to create memory for both a struct Idev and the string q --- a_var.idev_ptr->q. Look for the problem to originate in foo.
Segmentation faults are caused by trying to reference a memory address that is not allocated to your process....

I put your code inside code tags, otherwise it is very hard to read.

I got the Seg-11 even after conditional checking for NULL before actually accessing the struct object --:

        if\(a\_var.idev_ptr != NULL\)
\{
/*Access to this is pretty fine*/
	i_var = a\_var.idev_ptr->p;
	
/*But accessing array element gives the segmentation fault*/
	strncpy\(str, a\_var.idev_ptr->q, 10\);
\}

This is perticularly strange to me.

The actual code for which foo() is a dummy is written in a .so file and the library is supposed to provide me the *idev_ptr populated via some malloc() and user is supposed to free(idev_ptr).

Its starange.

Have you insured that the definitions for the structure are identical between the library where foo resides and whoever is calling it? You may try recompiling both with the same header if both sources are under your control just to be sure.

Make sure that a_var.idev_ptr->q != NULL.
<edit: nevermind on the sizeof stuff regarding str -- statically alloc'd array -- sizeof works fine.>
Also your free has a syntax error with variable name.