Thread: Pointer Basics

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    8

    Pointer Basics

    Hello! Could anyone figure out what's wrong with the following code?
    :
    Code:
    #include <stdio.h>
    int main()
    {
    
        char s[]="raja";
        char *t;
        t=s;
        printf("t=%d\ns=%d\n\n",t,s);
            
            while(*s!='\0')
            {
                *t=*s;
                printf("%c",*t);
                s++;     //i get an error msg at this line "lvalue required as increment operand"// 
                t++;
    
            }
        
        return 0;
    }
    :
    Thanks!

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    s is an array of char. Arrays, unlike pointers, cannot be incremented.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    And the other glaring problem is you are asking for help with C code in a C++ forum.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    1
    AndrewHunter i am not so sure about the last bit as this is the C Programming section or has this has just been moved here?

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Gooey View Post
    AndrewHunter i am not so sure about the last bit as this is the C Programming section or has this has just been moved here?
    It was moved.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    Sep 2011
    Posts
    8
    Oops!!My bad....Sorry for the trouble guys.

  7. #7
    Registered User gardhr's Avatar
    Join Date
    Apr 2011
    Posts
    151
    Quote Originally Posted by AndrewHunter View Post
    And the other glaring problem is you are asking for help with C code in a C++ forum.
    Maybe I'm being too pedantic here, but that's still valid C++ (using deprecated header files, of course).

  8. #8
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by gardhr View Post
    Maybe I'm being too pedantic here, but that's still valid C++ (using deprecated header files, of course).
    That would depend on what your definition of valid is.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  9. #9
    Registered User gardhr's Avatar
    Join Date
    Apr 2011
    Posts
    151
    Quote Originally Posted by AndrewHunter View Post
    That would depend on what your definition of valid is.
    The C standard library isn't just IO. It's comprised of functions which form a good portion of the core C++ facilities as well (math functions, character-type mapping, etc). Moreover, C++ IO is typically implemented with C IO, internally, too, so...your validity argument just doesn't hold water, frankly.

  10. #10
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    1. I was joking, hence the winky guy.
    2. It is not proper, nor suggested, C++ technique to use C style anything.
    3. No implementation of C++ reverts to C IO anymore, the first C++ compiler did however that was a long long time ago. Thus your argument doesn't hold water
    4. Before you start an argument when there isn't one, get your facts straight
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by gardhr View Post
    Moreover, C++ IO is typically implemented with C IO, internally, too, so...your validity argument just doesn't hold water, frankly.
    I really doubt that. The first C++ compiler was probably written in C, but once they got things going, I really doubt they just implemented all of their IO as C functions hidden by C++ calls. After all, once you can compile C++, you can rewrite the compiler as C++.


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

  12. #12
    Registered User gardhr's Avatar
    Join Date
    Apr 2011
    Posts
    151
    Quote Originally Posted by quzah View Post
    I really doubt that. The first C++ compiler was probably written in C, but once they got things going, I really doubt they just implemented all of their IO as C functions hidden by C++ calls. After all, once you can compile C++, you can rewrite the compiler as C++.

    Quzah.
    Right, but the only alternative is to use OS-specific calls, which is certainly less portable. If it were up to me, I'd just keep it simple and use C IO.

    Quote Originally Posted by AndrewHunter View Post
    It is not proper, nor suggested, C++ technique to use C style anything.
    The beauty of C++ is that it is a "multi-paradigm" programming language. As long as safe coding practices are being used, there shouldn't be any shame in using C-style constructs.

  13. #13
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by gardhr View Post
    Right, but the only alternative is to use OS-specific calls, which is certainly less portable. If it were up to me, I'd just keep it simple and use C IO.
    Look, the compiler libraries are OS specific. How do you think they print things to the screen, access memory, the harddrive, ect.? They do that by making system calls. You seem to have a huge misunderstanding here on how a compiler/linker work; I suggest you do some reading.

    Quote Originally Posted by gardhr View Post
    The beauty of C++ is that it is a "multi-paradigm" programming language. As long as safe coding practices are being used, there shouldn't be any shame in using C-style constructs.
    Wrong again, Bjarne Stroustrup himself has said so. You should read through his article on Learning C++ as a new programming language
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  14. #14
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by gardhr View Post
    Right, but the only alternative is to use OS-specific calls, which is certainly less portable. If it were up to me, I'd just keep it simple and use C IO.
    And what, prey tell, do you think the underpinnings of C I/O is?
    That's right... It's OS specific function calls.

    For example: fopen() is almost certainly using the Windows CreateFile() function, since that is how you open files in Windows.


    The beauty of C++ is that it is a "multi-paradigm" programming language. As long as safe coding practices are being used, there shouldn't be any shame in using C-style constructs.
    The beauty of C++, far as I'm concerned, is that it's generally discussed in a different forum.

  15. #15
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by CommonTater View Post
    The beauty of C++, far as I'm concerned, is that it's generally discussed in a different forum.
    Haha...brilliant! G'Day tater.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help basics %n
    By dstompy in forum C Programming
    Replies: 4
    Last Post: 05-06-2011, 04:29 PM
  2. basics
    By kocika73 in forum C++ Programming
    Replies: 9
    Last Post: 03-10-2006, 11:28 AM
  3. need help with basics!
    By teevee in forum C Programming
    Replies: 10
    Last Post: 01-29-2006, 06:55 AM
  4. I've got the basics down...
    By gcn_zelda in forum C++ Programming
    Replies: 10
    Last Post: 05-24-2003, 09:10 AM
  5. OO Basics
    By DISGUISED in forum C++ Programming
    Replies: 3
    Last Post: 01-04-2002, 07:25 PM