how to Increment pointers with in a structure

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Yuga
    New Member
    • Feb 2007
    • 6

    how to Increment pointers with in a structure

    Hi,

    I have a question,....he re is my program
    Code:
    struct list_el {
       int val;
       TEXT *acct_no;
       struct list_el * next;
    };
    
    typedef struct list_el item;
    
    void main() {
       item * curr, * head;
       int i;
       long j = 1000;
       char ch[4];
    
       head = NULL;
    
       for(i=1;i<=10;i++) {
          curr = (item *)malloc(sizeof(item));
          curr->val = i;
          sprintf(ch,"%ld",j);
          curr->acct_no     =       (pTEXT)ch;
          curr->next  = head;
          head = curr;
    
    j +=    500;
       }
    
       curr = head;
    
       while(curr) {
          printf("%s \t %d\n", curr->acct_no, curr->val);
          curr = curr->next ;
       }
    free(curr);
    }
    o/p is
    5500 10
    5500 9
    5500 8
    5500 7
    5500 6
    5500 5
    5500 4
    5500 3
    5500 2
    5500 1

    Expected is
    1000 10
    1500 11

    Here TEXT is 4-byte unsigned integer (Character pointers to type TEXT).

    Could you please help me to figure out this error? I am just a starter in C.
    Hope my question is clear....

    Thanks in advance..
    Last edited by horace1; Mar 2 '07, 07:25 AM. Reason: added code tags
  • DeMan
    Top Contributor
    • Nov 2006
    • 1799

    #2
    I think (and correct me when I'm wrong) that you want
    Code:
    *(curr->acct_no)  = (pTEXT)ch; //you want the value at the pointer to acct_no, not the pointer itself
    Although your result is interesting, because 5500 is what we would expect the LAST value to be so I probably missed something

    Comment

    • horace1
      Recognized Expert Top Contributor
      • Nov 2006
      • 1510

      #3
      in the loop
      Code:
         for(i=1;i<=10;i++) {
            curr = (item *)malloc(sizeof(item));
            curr->val = i;
            sprintf(ch,"%ld",j);
            curr->acct_no     =       (pTEXT)ch;
            curr->next  = head;
            head = curr;
      
      j +=    500;
         }
      j increments from 1000 to 5500 and on exit ch[] contains the string "5500". Each element of your list contains a pointer to ch[] so they ALL point to the same string, i.e. "5500".

      you can have a char array in your structure an copy the string into it
      Code:
      struct list_el {
         int val;
         char acct_no[5];
         struct list_el * next;
      };
      note ch[] should be ch[5], 4 elements for the numeric characters and one for the terminating null character '\0'.
      Otherwise acct_no could be an int

      Comment

      • chella
        New Member
        • Mar 2007
        • 51

        #4
        Originally posted by Yuga
        Hi,

        I have a question,....he re is my program
        Code:
        struct list_el {
           int val;
           TEXT *acct_no;
           struct list_el * next;
        };
        
        typedef struct list_el item;
        
        void main() {
           item * curr, * head;
           int i;
           long j = 1000;
           char ch[4];
        
           head = NULL;
        
           for(i=1;i<=10;i++) {
              curr = (item *)malloc(sizeof(item));
              curr->val = i;
              sprintf(ch,"%ld",j);
              curr->acct_no     =       (pTEXT)ch;
              curr->next  = head;
              head = curr;
        
        j +=    500;
           }
        
           curr = head;
        
           while(curr) {
              printf("%s \t %d\n", curr->acct_no, curr->val);
              curr = curr->next ;
           }
        free(curr);
        }
        o/p is
        5500 10
        5500 9
        5500 8
        5500 7
        5500 6
        5500 5
        5500 4
        5500 3
        5500 2
        5500 1

        Expected is
        1000 10
        1500 11

        Here TEXT is 4-byte unsigned integer (Character pointers to type TEXT).

        Could you please help me to figure out this error? I am just a starter in C.
        Hope my question is clear....

        Thanks in advance..

        Hi yuga,
        Is this the output u expect?

        1000 1
        1500 2
        2000 3
        .
        .
        .
        .
        5500 10

        Regards,
        Chella

        Comment

        • willakawill
          Top Contributor
          • Oct 2006
          • 1646

          #5
          Hi. This is probably closer to your expected output:
          Code:
          struct list_el {
             int val;
             char acct_no[5];
             struct list_el * next;
          };
          
          typedef struct list_el item;
          
          int main() {
             item * curr, * head;
             int i;
             long j = 1000;
             char ch[5];
          
             head = NULL;
          
             for(i=1;i<=10;i++) {
                curr = (item *)malloc(sizeof(item));
                curr->val = i;
                sprintf(ch,"%d",j);
                strcpy(curr->acct_no, ch);
                curr->next  = head;
                head = curr;
          
          		j +=    500;
             }
          
             curr = head;
          
             while(curr) {
                printf("%s \t %d\n", curr->acct_no, curr->val);
                curr = curr->next ;
             }
          	free(curr);
          	getchar();
          	return 0;
          }
          I eliminated any possible errors from your TEXT declaration and use strcpy() instead of
          Code:
          curr->acct_no = ch
          which points all of your curr->acct_no pointers to the memory space that ch points to, so they all equal the final value stored there which is 5500.
          ch, by the way, needs to be able to store 5 characters; the four from j and a null terminator.. main should always return an int

          Well look at that! We all answered at once :)

          Comment

          • Yuga
            New Member
            • Feb 2007
            • 6

            #6
            Excellent response ....once again thanks for your time...

            Is it possible to use TEXT *acct_no in the above structure instead of char acct_no[5]. Because I need to pass these values to CTree API functions which accept only TEXT data types.

            Comment

            • willakawill
              Top Contributor
              • Oct 2006
              • 1646

              #7
              Originally posted by Yuga
              Excellent response ....once again thanks for your time...

              Is it possible to use TEXT *acct_no in the above structure instead of char acct_no[5]. Because I need to pass these values to CTree API functions which accept only TEXT data types.
              There is an easy way to find out. Suck it and see
              I did not have your declaration for this data type so I avoided it.

              Comment

              Working...