Results 1 to 8 of 8

Thread: K&R's strcpy function - problem with pointers

  1. #1
    Join Date
    Jan 2006
    Location
    11 N 78 E
    Posts
    110
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Smile K&R's strcpy function - problem with pointers

    I can understand the coding:
    Qt Code:
    1. void strcpy(char *s, char *t) {
    2. int i = 0;
    3. while ((s[i] = t[i]) != '\0') i++; }
    To copy to clipboard, switch view to plain text mode 
    but if it is changed to:
    Qt Code:
    1. void strcpy(char *s, char *t) {
    2. while ((*s = *t) != '\0') { s++; t++; } }
    To copy to clipboard, switch view to plain text mode 
    then I do not understand it.

    In the first implementation, if someone calls strcpy(pname, star); then the function is passed two pointers to the values contained by the variables pname and star (which were previously declared as static char arrays BTW). Since we have told the function by the argument declaration (char *s, char *t) that its input is pointers, the function understands that the actual memory locations are to be updated and this is done by s[i] = t[i].

    But in the second implementation what does *s = *t mean? How can first of all *s be used here? Since the declaration is strcopy (char *s, char *t) the variable names s and t refer to the same memory location as pname and star, but then, pname and star were not passed by reference. (No & there.) [In this case, I see that I should be questioning how the first implementation itself worked.] I am getting confused...

    Obviously, I am a relative newbie to C and C++. Kindly be patient. Thanks.
    Penguin #395953 using Qt for open-source development on X11 using C++ and
    Python via PyQt

  2. #2
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: K&R's strcpy function - problem with pointers

    s and t are the pointers whereas *s and *t are the values.(Remember * gives you the value).

    Therefore,

    *s = *t copies the values and initially s and t are pointing to s[0] and t[0] respectively.

    s++ and t++ increments the pointer and therefore now *s and *t gives you the values of s[1] and t[1].

    Hope its clear
    cheers.

  3. #3
    Join Date
    Jan 2006
    Location
    11 N 78 E
    Posts
    110
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: K&R's strcpy function - problem with pointers

    s and t are the pointers whereas *s and *t are the values.(Remember * gives you the value).
    So when strcpy(a,b) is executed, the values of a and b are passed to the function, and the function matches its argument declaration (char *s, char *t) to (a, b), so s and t are those pointers whose values (extracted by *) are those passed from a and b.

    All right, now that's clear, but now how does s[i] = t[i] work in the first example? The pointer is not an array, right? So how can t[i] be a valid expression let alone get assigned to s[i]? If it were *s[i] = *t[i] it would be understandable...
    Penguin #395953 using Qt for open-source development on X11 using C++ and
    Python via PyQt

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: K&R's strcpy function - problem with pointers

    Quote Originally Posted by jamadagni
    The pointer is not an array, right?
    That's the problem with arrays in C and C++ --- you can treat them as pointers to first element.

    In fact x[i] is the same as *(x + i).

    Maybe this will shed some light:
    http://www.eskimo.com/~scs/cclass/notes/sx10b.html

  5. #5
    Join Date
    Jan 2006
    Location
    11 N 78 E
    Posts
    110
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: K&R's strcpy function - problem with pointers

    Quote Originally Posted by jacek
    In fact x[i] is the same as *(x + i).
    But only if x is a pointer, right? For example, this will not be true: if char s[]="hello"; then s[3] = *(s + 3) -- correct?
    Penguin #395953 using Qt for open-source development on X11 using C++ and
    Python via PyQt

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: K&R's strcpy function - problem with pointers

    Quote Originally Posted by jamadagni
    For example, this will not be true: if char s[]="hello"; then s[3] = *(s + 3) -- correct?
    No, s is also a pointer. This inconsistency is a real problem in C and C++.

    Qt Code:
    1. #include <stdio.h>
    2.  
    3. int main()
    4. {
    5. char s[] = "hello";
    6.  
    7. printf( "%c\n", s[3] );
    8. printf( "%c\n", *(s+3) );
    9.  
    10. return 0;
    11. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Jan 2006
    Location
    11 N 78 E
    Posts
    110
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: K&R's strcpy function - problem with pointers

    Quote Originally Posted by jacek
    No, s is also a pointer. This inconsistency is a real problem in C and C++.
    Why you call this an inconsistency? Just now I read in K&R:
    By definition the value of a variable or expression of type array is the address of element zero of the array. Thus after the assignment pa = &a[0]; pa and a have identical values. Since the name of an array is a synonym for the location of the initial element, the assignment pa = &a[0]; can also be written as pa = a;
    Penguin #395953 using Qt for open-source development on X11 using C++ and
    Python via PyQt

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: K&R's strcpy function - problem with pointers

    Quote Originally Posted by jamadagni
    Why you call this an inconsistency?
    Don't you feel confused when you learn how arrays work in C? Compare arrays from C with those in Pascal or Java.

    Quote Originally Posted by jamadagni
    Just now I read in K&R:
    You know, they wouldn't write something like "arrays are more a hack than a real data type".

Similar Threads

  1. QPSQL problem
    By LoneWolf in forum Installation and Deployment
    Replies: 60
    Last Post: 4th November 2009, 15:22
  2. Problems passing an array of pointers to objects to a function
    By Valheru in forum General Programming
    Replies: 16
    Last Post: 30th June 2008, 01:11
  3. how to add static library into qmake
    By Namrata in forum Qt Tools
    Replies: 1
    Last Post: 20th November 2007, 18:33
  4. Problem emitting signal from a static function
    By Valheru in forum Qt Programming
    Replies: 21
    Last Post: 12th June 2007, 15:48
  5. I got two problems when I used static compiled library of QT4
    By qintm in forum Installation and Deployment
    Replies: 8
    Last Post: 20th April 2006, 09:52

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.