Wrong access for char *string[]

This is a discussion on Wrong access for char *string[] within the C Programming forums, part of the General Programming Boards category; Hello, I am writing here because my application have unexpected function result. This function reverse the string content and lower ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    20

    Wrong access for char *string[]

    Hello,

    I am writing here because my application have unexpected
    function result.

    This function reverse the string content and lower case. For example "AZERTY" is the source then "ytreza" will be the result that I hope to get.

    I do not put here the entire code then I summarize it:

    Code:
    #include <ctype.h>
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
     char kbuff[255];
     parser( &kbuff[0] );
    }
    
    
    int parser( char *kbuff[255] )
    
    {
      int i =0;
      int j;
      char kbufft[15];
    
      kbufft[0] = '\0';
      j = strlen (kbuff) -1;
    
      while (  ! ( (i>=5) || (j<0) ) )
            {
       	  kbufft[i] = tolower ( (chr) kbuff[j] );
    	  kbufft[i+1]= '\0';
    	  i++;
    	  j--;
    	  printf("my reversed, lowered string is:%s \n", kbufft);
    	}
    
    
    }
    I neither use * nor & in my program because I can get value without.

    The problem are: kbuff[j] works well only for the last char "y" not for all next. Then, kbufft have 0 length or it is fullfilled with the "y" char.

    I conclude that it is impossible to access one by one character in
    the char *kbuff variable. To solve the problem I have to create a second variable kbuffx[] inside the function then copy the original to it, I have to work on the copy not the original:

    strpcy(kbuffx, kbuff);

    My question is why it is impossible to access each character in
    pointed string ? The solution I gave is not a good idea because creating a new variable spend memory space.

    Does tolower() function cause the problem ?

    I think that the memory address of kbuff[] change each time and access element of it is not possible ( but the entire string access is possible).

    My system is P4 under LINUX Slackware 10.1 with GCC / GLIBC

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Your function doesn't need to take a pointer to an array. Just pass it the array. What you've got as an argument is: "An array, with 255 members, of pointers to type char." Read it from right to left.

    Just pass the array name to the function, and lose the * in the prototype, and try there.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    20
    I have followed your recommendation then this is the new code
    cleaner. I remove the tolower() function to simplify but the problem remains:
    as result special/unreadable character stored in kbufft .

    Why the pointer "point" give wrong data ?

    Code:
    #include <ctype.h>
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
     char kbuff[15];
     parser( &kbuff );
    }
    
    int parser( char *kbuff )
    
    {
      char i = 0;  // This  fill the kbufft[i]
      cahr j;        // This is the range 0 to xxxxx to read the source kbuff
      char kbufft[15]; // This contains the result: string reversed of kbuff
      char *kbuffz[15];  // This is an array pointed to kbuff[0] ,
    		   // I need this to obtain the length of kbuff
      char *point;	   // This a char pointed to end of kbuff (just before the '\0')
      
      kbufft[0] = '\0';
      kbuffz[0]=kbuff;
      j = strlen (*kbuffz) -1;
      point= kbuff[j];
    
      while (  ( (i<=14) && (j>=0) ) )
            {
       	  kbufft[i] = *point;
    	  kbufft[i+1]= '\0';
    	  point--;
    	  i++;
    	  j--;
    	}
      printf("\nMy reversed, string is:%s \n", kbufft);
    }
    Last edited by intmail; 11-09-2005 at 04:57 AM.

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    29
    You are still passing a pointer to a pointer to function parser...
    Code:
    int main(void)
    {
     char kbuff[15];
     parser( &kbuff );
    }

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    20
    >csisz3r

    kbuff is not a pointer in the main program.
    kbuff is not the same as *kbuff and &kbuff.
    The name are identical but *kbuff is pointer only available inside he function.
    They are treated differently even the name are same.
    Last edited by intmail; 11-10-2005 at 12:58 AM.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You're missing the point, clearly. Go read this FAQ.

    1) An array name, 'kbuff' is treated as a pointer to its first element. As such, all you should be doing is passing 'kbuff' to the function, as you've been told multiple times now.

    2) You're right. It's not, so stop doing it wrong, and start listening. Just pass 'kbuff' to the function. Nothing else.

    3) Wrong. Arrays are passed as pointers to their first element. Any change to a passed array that happen inside a function will affect the array outside the function. Don't believe us still? Test it yourself:
    Code:
    #include<stdio.h>
    void fun( int array[3] )
    {
        array[0] = 10;
        array[1] = 21;
        array[2] = 32;
    }
    
    int main( void )
    {
        int array[3] = { 1, 2, 3 };
        int x;
    
        printf("Displaying array contents:\n");
        for( x = 0; x < 3; x++ )
            printf("array[ %d ] is %d\n", x, array[ x ] );
    
        printf("Calling 'fun( array )'\n" );
        fun( array );
    
        printf("Displaying array contents:\n");
        for( x = 0; x < 3; x++ )
            printf("array[ %d ] is %d\n", x, array[ x ] );
    
        return 0;
    }
    
    /*
        Output for the lazy, or compiler deprived:
    
    Displaying array contents:
    array[ 0 ] is 1
    array[ 1 ] is 2
    array[ 2 ] is 3
    Calling 'fun( array )'
    Displaying array contents:
    array[ 0 ] is 10
    array[ 1 ] is 21
    array[ 2 ] is 32
    
    */
    4) No, not really they aren't. Not in this case.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    20
    >quzah and all

    Thank you for your help. Finally I will not use pointer in string/arry because it is very difficult to manage it. Really I do not need pointer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 11:01 AM
  2. Access books, tutorials, source
    By maes in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 04-17-2002, 03:43 PM
  3. Direct disk access in DOS
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 02-26-2002, 01:52 PM
  4. using DAO without MS Access installed
    By zMan in forum Windows Programming
    Replies: 2
    Last Post: 02-20-2002, 02:14 PM
  5. 0xC0000005: Access Violation
    By Strider in forum Windows Programming
    Replies: 3
    Last Post: 11-07-2001, 01:46 PM

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