#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    2
    Rep Power
    0

    An array of string within a struct contained in another array of structs w/n a struct


    Hello,

    I have a question to ask.

    I encountered an error in getting the value of
    temp->spCon[k]>strPinName[m] .

    The structs are:
    PHP Code:
    struct sConnections{

        
    charsN;

        
    charsM;

       
    charstrNetName[20];

       
    charstrPinName[20];

    };

    struct sInstAll{

       
    struct sInstancespInst;

       
    charstrProp;

       
    struct sConnectionsspCon[20];

       
    struct sInstAll *next;

    }; 
    And this doesn't work:

    PHP Code:
    sNetName _strdup(temp->spCon[k]->strNetName[m]);
                                    
    sPinName _strdup(temp->spCon[k]->strPinName[m]);
                                    
    fprintf(fpout"%s %s ",sNetNamesPinName); 

    Even if I have this on the watch:

    PHP Code:
    +temp->spCon[k]->strNetName[m]    0x001acd38 "orange" char *
    +
    temp->spCon[k]->strPinName[m]    0x001acd80 "c"    char 
    Help.
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,392
    Rep Power
    1871
    Perhaps you should clarify "it doesn't work", which doesn't actually tell us anything useful.
    http://www.chiark.greenend.org.uk/~sgtatham/bugs.html

    Does it compile?
    Does it crash always, or only on specific inputs?
    Does it produce garbage answers?
    etc

    Code:
     sNetName = _strdup(temp->spCon[k]->strNetName[m]);
                                    
    sPinName = _strdup(temp->spCon[k]->strPinName[m]);
                                    
    fprintf(fpout, "%s %s ",sNetName, sPinName);
    Why do you make copies of the strings, just to print them?
    Do you free those copies immediately afterwards?

    To debug a crash effectively, we really need the whole code and the input data. Random snippets of "I think this line of code is the problem" are both useless and wrong.

    Almost certainly, the real problem is somewhere else in the code, and your 'guess' just happens to be where those seeds of destruction finally germinate.

    I'd suggest you first check your array indices are within bounds, and then check back that you allocated enough memory at each instance.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    2
    Rep Power
    0
    The code compiles.

    I am using Visual Studio 2012.

    The other members of the struct temp prints, e.g. strProp, but spCon w/c is struct containing another array does not print.

    I copied the string to a temp char* storage just to see whether it copies the value, but it seems like it does not. Before, I directly placed temp->spCon[k]->strPinName[m] w/in the fprintf statement but it also didn't work.

IMN logo majestic logo threadwatch logo seochat tools logo