Problem while accessing array of structure

Joined
Mar 22, 2009
Messages
1
Reaction score
0
please read the code below and then help me with the problem stated at the end.

Code:
typedef struct {
char* hostname;
int connects;
} Str;

char * getandprint(int *argcnt, Str * strarr)
{
        static char buff[8];
        int i,k;
        Str *strarr1=0;
        strcpy(buff,"PRINTER"); //this will be returned
        *argcnt=getcount();
        printf("count is %d\n",*argcnt);
        for (i=0;i<(*argcnt);i++)
        {
                strarr1=(Str *)malloc(sizeof(Str));
                if(strarr1 !=(Str *)0){
                        memset(strarr1,0,sizeof(Str));
                        seqarr1=GetStr();
                        if(seqarr1 !=(Str *)0) {
                                strarr[i]=*strarr1;
                                printf("outside %d string is %s\n",seqarr[i].connects,seqarr[i].hostname);
                        }
                }
        }
        for (i=0;i<(*argcnt);i++)
        {
                printf("printed %d string is %s\n",seqarr[i].connects,seqarr[i].hostname);
        }
        return buff;
}

Str * GetStr()
{
//This dynamically allocates the memory and then populates Str element and returns pointer to it
}

I am able to print everything correctly in the printf of first for loop.

However, the printf in second for loop gives following problems if *argcnt is large:
1) Some of the initial members print "seqarr.connects" correctly but junk values in "seqarr.hostname".
2) The program gives a segmentation fault if the count is very large.

For example, if count is 3000, for first 45-50 times, seqarr.connects is printed correctly but "seqarr.hostname" prints junk.
And after printing some first 2950 times, I get a segmentation fault.

Please let me know why am I getting these errors and how do I correct them?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top