Not accessing my pointers correctly?

This is a discussion on Not accessing my pointers correctly? within the C Programming forums, part of the General Programming Boards category; I have written this bit of code Code: char *mystrcpy(char * s1, const char * s2) { //Copies s2 into ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    3

    Not accessing my pointers correctly?

    I have written this bit of code
    Code:
    char *mystrcpy(char * s1, const char * s2)
    {
      //Copies s2 into s1, including the null terminating character
      int tick = 0;
      while(*s2[tick] != 0)
      {
    	  *s1[tick] = *s2[tick];
    	  tick++;
      }
      *s1[tick] = *s2[tick];
      return s1;
    }
    And I am getting this error when ever I deal with my pointers:
    invalid type argument of `unary *'

    I cant find anything wrong with it.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,699
    Think of it this way:

    - s1 is an address (pointer).
    - *s1 is the value at that address. You could also write that as *( s1 + 0 )
    - s1[ 0 ] is the same as *( s1 + 0 )
    - s1[ 1 ] is the same as *( s1 + 1 )

    So obviously *s[ 0 ] is trying to dereference a value (not a pointer), which of course, does not make sense.

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    3
    so are you saying that s[x] is already dereferencing the pointer?

  4. #4
    MTK
    MTK is offline
    Registered User
    Join Date
    Aug 2009
    Posts
    198
    That's right.

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,699
    Right, it basically means "add ( x * sizeof( char ) ) to the base address 's' and dereference. It follows then that:

    ( s + 0 ) == s
    &s[ 0 ] == ( s + 0 )

    ...etc...

  6. #6
    Registered User
    Join Date
    Sep 2009
    Posts
    3
    Thanks for all of your help guys.

  7. #7
    MTK
    MTK is offline
    Registered User
    Join Date
    Aug 2009
    Posts
    198
    Remember, however, that a multidimensional array is NOT an array of pointers! So this:

    **ptr

    Is not the same as:

    ptr[0][0]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Am I using pointers correctly here?
    By kisiellll in forum C Programming
    Replies: 4
    Last Post: 03-22-2009, 11:28 AM
  2. Pointers are not working correctly
    By adrian_fpd in forum C Programming
    Replies: 8
    Last Post: 11-17-2008, 06:55 PM
  3. Variable pointers and function pointers
    By Luciferek in forum C++ Programming
    Replies: 11
    Last Post: 08-02-2008, 02:04 AM
  4. MergeSort with array of pointers
    By lionheart in forum C Programming
    Replies: 18
    Last Post: 08-01-2008, 10:23 AM
  5. moving pointers to pointers
    By Benzakhar in forum C++ Programming
    Replies: 9
    Last Post: 12-27-2003, 07:30 AM

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