pointer in for loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • crumbs12
    New Member
    • Dec 2008
    • 7

    pointer in for loop

    hello friends,
    I am having a problem.

    This code works just fine when I print as soon as I assign the string to
    the pointer.

    Code:

    ptr->main = malloc(sizeof(d etail)*(660)+1) ;
    strcpy(ptr->main[count].key,token[i]);
    printf("key key %d %s ",count, ptr->main[count].key);But when I try to do this....

    Code:
          for(i=0; i < count; i++)
       {
              printf("%d %s %s \n", i,ptr->main[count].key);
          }It doesnt work.
    What am I doing wrong in the for loop...
    Code:
    typedef structn ARRAY1{
            char time[ARRAY_SIZE];
            char date[ARRAY_SIZE];
    }ARRAY1,ARR2;
     
    typedef struct first{
          char name[10];
           ARRAY1 *main;
       }first;
     
    typedef struct second{
          char db[10];
            ARRAY2 *other;
        }second;
    in main{
      FILE *data_txt,
            struct ARRAY1 *str_ptr;   
            struct ARR2 *new_ptr;
          str_ptr = &ARRAY1_info;
          new_ptr = &ARR2_info;
     
           struct first *ptr;
           struct second *new;
    
         data_txt = fopen("data.txt","r");
    
     while (fgets(line,100,data_txt)!=NULL)
     {
       
    
    
    trim(line);
     strcpy(first_info.name,"ccmlmd");
            if ((strlen(line) > 1)&&(read!=0)){
            lcount++;
            tokens = split(line, delim);
            for(i = 0; tokens[i] != NULL; i++) {
            if (i==0){
            if (tokens[i]!=NULL){
    ptr->main = malloc(sizeof(detail)*(660)+1);
            strcpy(ptr->main[count].key,token[i]);
          printf("key key %d %s ",count, ptr->main[count].key);
           }
          }
    
       }
      for(i = 0; tokens[i] != NULL; i++)
      free(tokens[i]);
      free(tokens);
      count++;
      lcount++;
          }
     
    // This doesnt work
          for(i=0; i < count; i++) {
     printf("%d %s %s", i,ptr->main[i].time, ptr->main[i].date);
          }
  • debasisdas
    Recognized Expert Expert
    • Dec 2006
    • 8119

    #2
    Question moved to C / C++ Forum.

    Comment

    • boxfish
      Recognized Expert Contributor
      • Mar 2008
      • 469

      #3
      In what way does it not work? If it gives you compiler errors, please post them here. If it gives you the wrong output, please post it here, along with the expected output. This will make it much easier for people to help you. Thanks.

      Comment

      • crumbs12
        New Member
        • Dec 2008
        • 7

        #4
        Basically this doesnt print anything
        Code:
         for(i=0; i < count; i++) { 
         printf("%d %s %s", i,ptr->main[i].time, ptr->main[i].date); 
              }

        Comment

        • RedSon
          Recognized Expert Expert
          • Jan 2007
          • 4980

          #5
          Have you tried debugging it? What does printf return to you?

          Comment

          • crumbs12
            New Member
            • Dec 2008
            • 7

            #6
            The printf in the for loop prints only this
            Code:
            0   
            1   
            2
            But when further up in the code prints just fine.
            Code:
            ptr->main = malloc(sizeof(detail)*(660)+1); 
                    strcpy(ptr->main[count].key,token[i]); 
                  printf("key key %d %s ",count, ptr->main[count].key);
            This prints just fine.

            Only problem is that this part of the code doesnt work
            Code:
                  for(i=0; i < count; i++) { 
             printf("%d %s %s", i,ptr->main[i].time, ptr->main[i].date); 
                  }

            Comment

            • RedSon
              Recognized Expert Expert
              • Jan 2007
              • 4980

              #7
              what type of data is ptr->main[i].time and ptr->main[i].date?

              Comment

              • crumbs12
                New Member
                • Dec 2008
                • 7

                #8
                As I posted above
                Code:
                typedef structn ARRAY1{ 
                        char time[ARRAY_SIZE]; 
                        char date[ARRAY_SIZE]; 
                }ARRAY1,ARR2; 
                  
                typedef struct first{ 
                      char name[10]; 
                       ARRAY1 *main; 
                   }first; 
                  
                typedef struct second{ 
                      char db[10]; 
                        ARRAY2 *other; 
                    }second;
                Thats the info I am trying to catch

                Comment

                • boxfish
                  Recognized Expert Contributor
                  • Mar 2008
                  • 469

                  #9
                  It's hard for me to tell what's wrong with your program because the code you posted has typos and does not compile. Two problems I can see are that in your posted code, you have not initialized ptr->main[i].time or ptr->main[i].date, and the line you posted separately,
                  Code:
                  printf("%d %s %s \n", i,ptr->main[count].key);
                  has one too many %s, but I don't know if these things are causing problems for you.

                  Comment

                  • crumbs12
                    New Member
                    • Dec 2008
                    • 7

                    #10
                    Oh ok...But since I have already initialized the pointer further up...wouldnt that be enough...or do I have to initialize the ptr again in this part of the code...?
                    Code:
                      for(i=0; i < count; i++) {  
                     printf("%d %s %s", i,ptr->main[i].time, ptr->main[i].date);  
                          }

                    Comment

                    • boxfish
                      Recognized Expert Contributor
                      • Mar 2008
                      • 469

                      #11
                      You do not initialize the date or time members in your code. You do initialize
                      Code:
                      ptr->main[count].key
                      but the ARRAY1 struct does not have a member called key. I'm afraid the code you posted does not make any sense.

                      Comment

                      • crumbs12
                        New Member
                        • Dec 2008
                        • 7

                        #12
                        I am sorry, please subsitite the 'key' and 'value' fields with 'time' and 'date'.

                        Comment

                        • RedSon
                          Recognized Expert Expert
                          • Jan 2007
                          • 4980

                          #13
                          Instead of making us substitute everything and try to force your code to work, why don't you provide a listing of your code that compiles so we can see what you are doing.

                          Comment

                          • crumbs12
                            New Member
                            • Dec 2008
                            • 7

                            #14
                            OK here is the corrected code.

                            Code:
                            typedef structn ARRAY1{
                                    char time[ARRAY_SIZE];
                                    char date[ARRAY_SIZE];
                            }ARRAY1,ARR2;
                             
                            typedef struct first{
                                  char name[10];
                                   ARRAY1 *main;
                               }first;
                             
                            typedef struct second{
                                  char db[10];
                                    ARRAY2 *other;
                                }second;
                            in main{
                              FILE *data_txt,
                                    struct ARRAY1 *str_ptr;   
                                    struct ARR2 *new_ptr;
                                  str_ptr = &ARRAY1_info;
                                  new_ptr = &ARR2_info;
                             
                                   struct first *ptr;
                                   struct second *new;
                            
                                 data_txt = fopen("data.txt","r");
                            
                             while (fgets(line,100,data_txt)!=NULL)
                             {
                               
                            
                            
                            trim(line);
                             strcpy(first_info.name,"ccmlmd");
                                    if ((strlen(line) > 1)&&(read!=0)){
                                    lcount++;
                                    tokens = split(line, delim);
                                    for(i = 0; tokens[i] != NULL; i++) {
                                    if (i==0){
                                    if (tokens[i]!=NULL){
                            ptr->main = malloc(sizeof(detail)*(660)+1);
                                    strcpy(ptr->main[count].time,token[i]);
                                  printf("time time %d %s ",count, ptr->main[count].time);
                                   }
                                  }
                            
                               }
                              for(i = 0; tokens[i] != NULL; i++)
                              free(tokens[i]);
                              free(tokens);
                              count++;
                              lcount++;
                                  }
                             
                            // This doesnt work
                                  for(i=0; i < count; i++) {
                             printf("%d %s ", i,ptr->main[i].time);
                                  }

                            Comment

                            • newb16
                              Contributor
                              • Jul 2008
                              • 687

                              #15
                              Originally posted by crumbs12
                              Oh ok...But since I have already initialized the pointer further up...wouldnt that be enough...or do I have to initialize the ptr again in this part of the code...?
                              Code:
                                for(i=0; i < count; i++) {  
                               printf("%d %s %s", i,ptr->main[i].time, ptr->main[i].date);  
                                    }
                              If it doesn't print anything, what is value of 'count'?

                              Comment

                              Working...