Quick question on pointers

This is a discussion on Quick question on pointers within the C Programming forums, part of the General Programming Boards category; If i have the a pointer called buf1 and it contains aload of data, is it ok to do the ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    58

    Quick question on pointers

    If i have the a pointer called buf1 and it contains aload of data, is it ok to do the following:


    Code:
    char *buf2
    
    memset(buf2, '/0' 70);
    strncpy(buf2, buf1, 70);

    I know you use memset with arrays but is it ok to do it with a pointer?

    Thanks

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    Assuming that buf2 has enough space for 70 chars, yes. But you should use '\0' and not '/0'.

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    58
    cheers always get those slashes wrong!

    buf2 will have enough space.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,821
    Ah, but of course that is colloquial speech. It is not buf2 that will have enough space, but the (dynamic) array whose first element buf2 points to that should have enough space.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question about pointers in C
    By Kilua in forum C Programming
    Replies: 3
    Last Post: 06-05-2009, 02:33 AM
  2. Quick Question: 2D Array with char **
    By Phoenix_Rebirth in forum C Programming
    Replies: 4
    Last Post: 01-29-2009, 06:33 AM
  3. Quick question about types...
    By Elysia in forum C++ Programming
    Replies: 32
    Last Post: 12-07-2008, 04:39 AM
  4. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM

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