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

    Join Date
    Aug 2003
    Location
    Istanbul, TR
    Posts
    85
    Rep Power
    11

    pointer array in while loop


    After i pass my first while loop the second loop makes its operations only once..Because the pointer array comes to its last element... How can i rewind it to the first element again?

    Code:
    while( (fgets(myelm, 1024, stream)) {
    
           blah blah blah some code here............
    
            while(*exptr != NULL) { // exptr is pointer array, char **exptr
                    if(strcmp(*exptr,myelm) == 0) {
                                    deg =1;
                                     continue;
                     }
                     *exptr++ 
    /* here i go to the other pointers in the pointer arr..  
    what i want is when i come to this second while loop again ,
    i want to start the loop with the first pointer in the pointer array
    and also dont want to use index like exptr[i]
    is there a rewind function or somthing like that?*/
              }
              if (deg == 1) 
                     continue;  //jump next element in first while if deg=1
    When i come to the second loop again the exptr normally reach its last element NULL.. And when i come to the second while loop i want loop begins with tthe first element of exptr..

    Thanks in advanced for any help..
  2. #2
  3. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    243
    You need to add '*exptr++ ' before your continue. You are doing an infinite loop once you have reached that point. If this is a typo you will need to provide actuall compileable code for help.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Istanbul, TR
    Posts
    85
    Rep Power
    11
    it doenst worked when i put *exptr++ before continue either but
    if i use an index (int pin) it worked as you said..

    what is the diffrence between these two codes they have to do the same thing am i right?


    below code works great.. i use index here
    Code:
     deg=0;
     pin =0;
     while(exptr[pin] != NULL) {
                if(strcmp(exptr[pin], myelm) == 0) {
                          deg =1;
                          pin++; // here is what you said
                          continue;
                 }
                 pin++; //if i dont go in if statement i have to increment this value again
      }
    
      if(deg == 1)
          continue;
    and below doesnt return anything , but this must do the same thing as above code..

    Code:
     deg=0;
     while(*exptr != NULL) {
                if(strcmp(*exptr,myelm) == 0) {
                            deg =1;
                          *exptr++;
                          continue;
                 }
                 *exptr++;
      }
    
      if(deg == 1)
          continue;
    what is the diffrence between these ,first one works great but second one returns nothing.. But my problem solved with the first code when i put pin++ before continue. Thanks for help...
  6. #4
  7. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    243
    Sorry, I didn't look at your code close enough the first time. To increment a pointer, you use just 'ptr++' not '*ptr++'. The second version increments what the pointer is pointing to (not sure in your case).

    Drop the '*' from your '*exptr++;' and it should work as expected.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Istanbul, TR
    Posts
    85
    Rep Power
    11
    if it was a standart pointer i had to increment it like you said but this one is pointer which points to a pointer..


    it is defined as
    char **exptr (similar to char *ptr[250])

    not
    char *exptr

    i have to increment that like *ptr++ i think..

    in exptr there are filepaths stored in it

    char *exptr[0]="/home/ppp0/file1";
    char *exptr[0]="/home/ppp0/file2";

    if you increment it as exptr++ segmantation fault occurs..

    i think it is better to use index to solve the problem.. thanx again for your help...

IMN logo majestic logo threadwatch logo seochat tools logo