Thread: Understanding Pointers

  1. #16
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Code:
         /* Replace characters after the first \0 with empty spaces */
            for(j=0; j<6; j++){
               for(i=0; i<7; i++){ 
                 if(scanned[j][i] == '\0') { 
                   for(k = i; k<7; k++){
                        scanned[j][k] = ' ';
                   }
                 }
               }
            }
    Your code and comment do not agree. Setting k = i is a mistake because you're also erasing the ith character, the \0 you should be keeping.

    You can prove it by taking your array of arrays and printing out all the colors like this:

    Code:
    for (i = 0; i < 7; i++) {
        printf("%s\n", scanned[i]);
    }
    It's undefined, so expect cuts from broken glass.

    It doesn't really matter what any left over characters might be, so I would either erase that bit the comment refers to, or fill the whole matrix with '\0' before you do input.

  2. #17
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by quzah View Post
    Not necessarily so.

    The writers of strcat aren't aware of if I pass it a single nul character, and I'm free to do so, because a single nul character is safely considered a string.
    Code:
    char buf[BUFSIZ] = {0}, n = 0;
    strcat( buf, &n );
    Perfectly legal.


    Quzah.
    Sure. But the arguments to strcat are still logically arrays, even when they are of size 1. The type system isn't aware of it, as you rightly point out, but it is useful to distinguish these two uses of pointers. And distinguish them both from abuses of pointers. A pointer to type T can be expected to point to either a single value of type T, or an array of type T.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #18
    Registered User
    Join Date
    Jun 2011
    Posts
    41
    Having more issues with the near finished program i'm making...

    I found out about strncmp and it simplified my project a lot.

    But now i'm getting weird errors when transferring my arrays as pointers to functions.

    Code:

    Code:
    #include <stdio.h>
    #include <stdbool.h>
    #include <ctype.h>
    #include <string.h>
    
    void get_colors(char (*scanned)[7]);
    bool colors_validate(const char (*scanned)[7], const char (*colors)[7]);
    void rvalue(const char (*scanned)[7], const char (*colors)[7]);
    void tvalue(const char (*scanned)[7], const char (*colors)[7]);
    void rtcvalue(const char (*scanned)[7], const char (*colors)[7]);
    
    int main(void){
     char scanned[6][7], answer; 
     char colors[12][7] = {"silver", "gold", "black", "brown", "red", "orange", "yellow", "green", "blue", "violet", "grey", "white"};
     int i;
    
         get_colors(scanned);
         if(colors_validate(scanned, colors)){
            rvalue(scanned, colors);
            tvalue(scanned, colors);
            rtcvalue(scanned, colors)
         }
         
         getch();
     return 0;   
    }
    
    void get_colors(char (*scanned)[7]){
         int i, j;
              
         printf("\nPlease enter 6 resistor colors: \n");
         scanf("%s %s %s %s %s %s", scanned[0], scanned[1], scanned[2], scanned[3], scanned[4], scanned[5]);
         while(getchar() != '\n');
         
         for(j=0; j<6; j++){
            for(i=0; i<7; i++){ 
               scanned[j][i] = tolower(scanned[j][i]); 
            }
         } 
    }
    
    bool colors_validate(const char (*scanned)[7], const char (*colors)[7]){
         int i, j;
         
         for(i = 0; i < 12; i++){
               for(j = 0; j < 6; j++){
                  if(strncmp(scanned[j], colors[i], 7) != 0){ return false; }
               }
         }
         
         if(strncmp(scanned[0], colors[2], 7) == 0 || strncmp(scanned[0], colors[1], 7) == 0 || strncmp(scanned[0], colors[0], 7) == 0){ printf("Wrong color. Try again\n"); return false; }
         else if(strncmp(scanned[1], colors[1], 7) == 0 || strncmp(scanned[1], colors[0], 7) == 0 || strncmp(scanned[2], colors[1], 7) == 0 || strncmp(scanned[2], colors[0], 7) == 0){ printf("Wrong color. Try again\n"); return false; }
         else if(strncmp(scanned[4], colors[11], 7) == 0) { printf("Wrong color. Try again\n"); return false; }
         else if(strncmp(scanned[5], colors[7], 7) == 0 || strncmp(scanned[5], colors[10], 7) == 0 || strncmp(scanned[5], colors[11], 7) == 0 || strncmp(scanned[5], colors[1], 7) == 0 || strncmp(scanned[5], colors[0], 7) == 0) { printf("Wrong color. Try again\n"); return false; }
         else return true;     
    }
    
    /* Find the Resistance value */
    void rvalue(const char (*scanned)[7], const char (*colors)[7]){
        int i;
        double rv, rval, power;
        
        for(i=0; i<4; i++){
                if ( strncmp(scanned[i], colors[0], 7) == 0 ) { rv = -2; power = .01; }
           else if ( strncmp(scanned[i], colors[1], 7) == 0 ) { rv = -1; power = .1;}
           else if ( strncmp(scanned[i], colors[2], 7) == 0 ) { rv = 0; power = 1;}
           else if ( strncmp(scanned[i], colors[3], 7) == 0 ) { rv = 1; power = 10;}
           else if ( strncmp(scanned[i], colors[4], 7) == 0 ) { rv = 2; power = 100;}
           else if ( strncmp(scanned[i], colors[5], 7) == 0 ) { rv = 3; power = 1000;}
           else if ( strncmp(scanned[i], colors[6], 7) == 0 ) { rv = 4; power = 10000;}
           else if ( strncmp(scanned[i], colors[7], 7) == 0 ) { rv = 5; power = 100000;}
           else if ( strncmp(scanned[i], colors[8], 7) == 0 ) { rv = 6; power = 1000000;}
           else if ( strncmp(scanned[i], colors[9], 7) == 0 ) { rv = 7; power = 10000000;}
           else if ( strncmp(scanned[i], colors[10], 7) == 0 ){ rv = 8; power = 100000000;}
           else if ( strncmp(scanned[i], colors[11], 7) == 0 ){ rv = 9; power = 1000000000;}
           
           if(i == 0){ rval = rv * 100; } 
           if(i == 1){ rval += rv * 10; } 
           if(i == 2){ rval += rv; }
           if(i == 3){ rval = rval * power; }
        }
        
      if(rval < 1000000){ printf("Resistance = %.2f kOhms\n", rval/1000); }
      else { printf("Resistance = %.2f MOhms\n", rval/1000000); }
    
    }
    
    /* Find the resistance tolerance value */
    void tvalue(const char (*scanned)[7], const char (*colors)[7]){
        double tv;
        
              if( strncmp(scanned[4], colors[3], 7) == 0){ tv = 1; }
         else if( strncmp(scanned[4], colors[4], 7) == 0){ tv = 2;   }
         else if( strncmp(scanned[4], colors[5], 7) == 0){ tv = 3;   }
         else if( strncmp(scanned[4], colors[6], 7) == 0){ tv = 4;   }
         else if( strncmp(scanned[4], colors[7], 7) == 0){ tv = .5;  }
         else if( strncmp(scanned[4], colors[8], 7) == 0){ tv = .25; }
         else if( strncmp(scanned[4], colors[9], 7) == 0){ tv = .1;  }
         else if( strncmp(scanned[4], colors[10],7) == 0){ tv = .05; }
         else if( strncmp(scanned[4], colors[1], 7) == 0){ tv = 5;   }
         else if( strncmp(scanned[4], colors[0], 7) == 0){ tv = 10;  }
         
         if(tv > 0){ printf("Tolerance = +/- %.2f%%\n", tv); }
         else { printf("Tolerance = +/- %.2f%%\n", tv); } 
        
       
    }
    
    /* Find the resistance temperature coefficient */
    void rtcvalue(const char (*scanned)[7], const char (*colors)[7]){
      int rtc;
       
              if(strncmp(scanned[5], colors[3], 7) == 0) { rtc = 100; }
         else if(strncmp(scanned[5], colors[4], 7) == 0) { rtc = 50; }
         else if(strncmp(scanned[5], colors[5], 7) == 0) { rtc = 15; }
         else if(strncmp(scanned[5], colors[6], 7) == 0) { rtc = 25; }
         else if(strncmp(scanned[5], colors[8], 7) == 0) { rtc = 10; }
         else if(strncmp(scanned[5], colors[9], 7) == 0) { rtc = 5; }
         
         printf("Tempco = %d ppm/degreeC\n", rtc);
    
    }
    Error output:
    for all my functions ; incompatible pointer type.... but i dont see what i did different..

  4. #19
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
        for(i=0; i<4; i++){
                if ( strncmp(scanned[i], colors[0], 7) == 0 ) { rv = -2; power = .01; }
           else if ( strncmp(scanned[i], colors[1], 7) == 0 ) { rv = -1; power = .1;}
           else if ( strncmp(scanned[i], colors[2], 7) == 0 ) { rv = 0; power = 1;}
           else if ( strncmp(scanned[i], colors[3], 7) == 0 ) { rv = 1; power = 10;}
           else if ( strncmp(scanned[i], colors[4], 7) == 0 ) { rv = 2; power = 100;}
           else if ( strncmp(scanned[i], colors[5], 7) == 0 ) { rv = 3; power = 1000;}
           else if ( strncmp(scanned[i], colors[6], 7) == 0 ) { rv = 4; power = 10000;}
           else if ( strncmp(scanned[i], colors[7], 7) == 0 ) { rv = 5; power = 100000;}
           else if ( strncmp(scanned[i], colors[8], 7) == 0 ) { rv = 6; power = 1000000;}
           else if ( strncmp(scanned[i], colors[9], 7) == 0 ) { rv = 7; power = 10000000;}
           else if ( strncmp(scanned[i], colors[10], 7) == 0 ){ rv = 8; power = 100000000;}
           else if ( strncmp(scanned[i], colors[11], 7) == 0 ){ rv = 9; power = 1000000000;}
           
           if(i == 0){ rval = rv * 100; } 
           if(i == 1){ rval += rv * 10; } 
           if(i == 2){ rval += rv; }
           if(i == 3){ rval = rval * power; }
        }
    Shouldn't you be breaking here if you get a match, instead of ignoring it and continuing to loop three more times?

    edit - I read that last block as your tolower, ignore that. But I think the word 'const' in there is what is throwing you off. What is it you are trying to make the constant? The pointer, or the characters being pointed to?


    Quzah.
    Last edited by quzah; 06-21-2011 at 02:54 PM.
    Hope is the first step on the road to disappointment.

  5. #20
    Registered User
    Join Date
    Jun 2011
    Posts
    41
    I just want the chars to be constant, not the pointer. The constant characters are not to be changed.. thus being a const.

    I can remove them, but it was advised by my teacher to make info that will not be edited to be made into a const.

  6. #21
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Code:
    rtcvalue(scanned, colors)
    Missing a semicolon here.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  7. #22
    Registered User
    Join Date
    Jun 2011
    Posts
    41
    yeah i caught the missing semicolon, but still getting for all my functions ; incompatible pointer type.

  8. #23
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well you can make colors a const char array without any issues, so that part's fine. You can't make scanned const, since that starts out uninitialized and changes at some point. So that means that for any parameter where you intend to use scanned, it cannot be const. const is not a function thing -- you can't use const to say "this function won't change this parameter". const means "this object can never change, by anybody anytime".

  9. #24
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    1. In some instances, your functions are expecting "const char" but you're passing them "char"s.
    2. You appear to be using "scanned" as both a "char" and a "const char."

    There's a few more things, but start with that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with understanding file pointers
    By hanniballector in forum C Programming
    Replies: 9
    Last Post: 06-30-2010, 04:07 PM
  2. Help with understanding Pointers...
    By kiddo in forum C Programming
    Replies: 12
    Last Post: 03-07-2010, 06:43 PM
  3. understanding pointers
    By princez90 in forum C Programming
    Replies: 14
    Last Post: 04-19-2008, 09:56 AM
  4. Help understanding arrays and pointers
    By James00 in forum C Programming
    Replies: 2
    Last Post: 05-27-2003, 01:41 AM
  5. understanding pointers in my prog
    By Unregistered in forum C Programming
    Replies: 0
    Last Post: 03-22-2002, 12:04 PM