changing array values using pointers

This is a discussion on changing array values using pointers within the C Programming forums, part of the General Programming Boards category; I am trying to traverse an array of chars with pointers. Every x characters, i want to make it a ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    20

    changing array values using pointers

    I am trying to traverse an array of chars with pointers. Every x characters, i want to make it a capitalized character. I have a pointer to the array and the array. The pointer is set to a const char* but the array is set to a char array so that i can change the values in the array. When i compile i get an error, assignment of read only location. Here is the code that i have that gives me the error:
    Code:
    void
    changeText(const char* text, int count){
      int counter = 0;
      for(int i=0; ;i++){
        if(!strncmp((text+i), "\0", 1)) break; //break when read null
        //check to see if its a character
        if(((int)*(text+i)>=65 && (int)*(text+i)<=90) ||
           ((int)*(text+i)>=97 && (int)*(text+i)<=122)){
          if((counter % count) == 0)
    	*(text+i) = toupper((int)(text+i));
          puts("Do something\n");
          counter++;
        }
        else{
          //make character a space
        }
      }
      puts("Called changeText()\n");
    }
    This is the line that gives me the error:
    Code:
    *(text+i) = toupper((int)(text+i));
    Here is the function call if that helps
    Code:
    const char* ptext;
    char text[MAX];
    ptext = &text;
    changeText(ptext, count);
    I don't understand why i getting this error if i'm not trying to change the value of the pointer itself, just want it points to. Am i getting confused by how pointers work?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,313
    Quote Originally Posted by dford425
    I don't understand why i getting this error if i'm not trying to change the value of the pointer itself, just want it points to. Am i getting confused by how pointers work?
    The problem is that you did not dereference the pointer, i.e., it should be:
    Code:
    *(text+i) = toupper((int)*(text+i));
    Actually, it can be simplified to:
    Code:
    text[i] = toupper(text[i]);
    Instead of manually performing the check to see if the current character is an alphabetic character, use isalpha. If you really must perform the check yourself, don't use magic numbers.

    Also, your use of strncmp is wholly unnecessary. Rather, instead of leaving the loop condition empty, check for text[i] != '\0'.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    This strikes me as overly complex...

    First casting text as an int is wrong. Int's usually take up more space so you may be producing effects you don't want to have.

    Second you could do this very easily with the traditional [] array indexing methods

    Finally your loop conditions are screwey....

    Code:
    changeText(char* text, int count)
      {
      int limit = strlen(text);
      for(int i=0; i < limit ;i++)
       {
        //check to see if its a character
        if(isalpha(text[i]))
          {
          if((i % count) == 0)
             text[i] = toupper(text[i]);    
          else                                               
            text[i] = ' ';                           
           }
        }
     }
    Last edited by CommonTater; 01-15-2011 at 01:12 PM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,313
    Another thing: you don't need the ptext variable when you call the function. You can simply write:
    Code:
    changeText(text, count);
    Note that the text pointer parameter in changeText points to a const char. Therefore, you cannot change the string in the calling function through it, so the comment "make character a space" is illogical. Actually, the function name itself is illogical, unless you make the text parameter a pointer to non-const char instead (in which case the name merely becomes not very descriptive; something like nthLetterToUpper might be better).

    CommonTater's suggestion of using strlen will work, except that it makes two passes through the string. Note also that there are several (deliberate?) mistakes in the example.
    Last edited by laserlight; 01-15-2011 at 01:09 PM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by laserlight View Post
    CommonTater's suggestion of using strlen will work, except that it makes two passes through the string. Note also that there are several (deliberate?) mistakes in the example.
    Naa... just in too big a hurry... Hopefully most of it's fixed now.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,313
    Well, if you want to provide a proper solution, I would have expected something like this:
    Code:
    void nthLetterToUpper(char *text, int n)
    {
        int count = 0;
        int i;
        assert(text && "null pointer not allowed");
        assert(n > 0);
        for (i = 0; text[i] != '\0'; ++i)
        {
            if (isalpha(text[i]))
            {
                ++count;
                if (count % n == 0)
                {
                    text[i] = toupper(text[i]);
                }
            }
            else
            {
                text[i] = ' ';
            }
        }
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by laserlight View Post
    Well, if you want to provide a proper solution, I would have expected something like this:
    Code:
    void nthLetterToUpper(char *text, int n)
    {
        int count = 0;
        int i;
        assert(text && "null pointer not allowed");
        assert(n > 0);
        for (i = 0; text[i] != '\0'; ++i)
        {
            if (isalpha(text[i]))
            {
                ++count;
                if (count % n == 0)
                {
                    text[i] = toupper(text[i]);
                }
            }
            else
            {
                text[i] = ' ';
            }
        }
    }
    Yep... this is turning into "one of those days".... Nice solution...

  8. #8
    Registered User
    Join Date
    Jan 2011
    Posts
    20
    Thanks for all the help. The reason i am using pointers instead of typical array reference is because i want to practice with pointers. Also when i was writing it, the toalpha function did not even cross my mind so thats for that tip.

    Thanks again for all your help.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,313
    Quote Originally Posted by dford425
    The reason i am using pointers instead of typical array reference is because i want to practice with pointers.
    Sure, but the way you are using pointer notation is pointless since the equivalent array notation is more readable. Either way, you are using a pointer since the parameter named text is a pointer. Here is my example with more sensible use of pointer notation:
    Code:
    void nthLetterToUpper(char *text, int n)
    {
        int count = 0;
        assert(text && "null pointer not allowed");
        assert(n > 0);
        for (; *text != '\0'; ++text)
        {
            if (isalpha(*text))
            {
                ++count;
                if (count % n == 0)
                {
                    *text = toupper(*text);
                }
            }
            else
            {
                *text = ' ';
            }
        }
    }
    Quote Originally Posted by dford425
    Also when i was writing it, the toalpha function did not even cross my mind so thats for that tip.
    isalpha, not toalpha
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array of pointers not changing out of scope?
    By LaffyTaffy in forum C++ Programming
    Replies: 1
    Last Post: 07-29-2010, 12:17 PM
  2. How to sort an array of pointers to structure
    By broli86 in forum C Programming
    Replies: 3
    Last Post: 06-30-2008, 02:52 PM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. accessing array of structs values
    By WaterNut in forum C++ Programming
    Replies: 12
    Last Post: 07-08-2004, 08:47 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21