Results 1 to 8 of 8

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

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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

    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

  2. #2
    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

  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

    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

  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
    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 

  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
    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

  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
    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, 14: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, 00:11
  3. how to add static library into qmake
    By Namrata in forum Qt Tools
    Replies: 1
    Last Post: 20th November 2007, 17:33
  4. Problem emitting signal from a static function
    By Valheru in forum Qt Programming
    Replies: 21
    Last Post: 12th June 2007, 14: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, 08: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.