Thread: strncpy adavnced :P

  1. #1
    Registered User
    Join Date
    Jul 2008
    Location
    The Netherlands
    Posts
    3

    Question strncpy adavnced :P

    Hello everybody,

    I just started to learn C, so I hope you don't mind that I will post here the newbie-problems I do run into. I am learning C as a hobby, just as I learned Basic, Pascal and VB(A) in the past. So now I want to learn the "famous C" :P.

    Well, here we go....

    You can use "strncpy' to copy a number of characters from one string to another. That is handy, but what I really would like is that I can copy a number of characters from a given position in the source string to the destination string. I tried some things....

    Having in mind that a string in C is an array of char, I tried this:

    strncpy(dest_str, source_str[3], 10)

    I hoped that strncpy would start at position 4 of the source string and copied 10 characters. But when I compiled the program, the compiler told me that it hated me and stopped :P.

    Then I thought that maybe a pointer could help me. So I tried the next something

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
    	char regel1[30];
    	char regel2[30];
    
    	char *ptr;
    
    	strcpy(regel1, "Programing is fun and relaxing to do");
    	
    
    	ptr = &regel1[10];
    	strncpy(regel2, *ptr, 3);
    	
    }
    But after that I got an casting error.

    Okay, so how to solve this problem... can I use "strncpy" at all to get done what I want? I mean, something like that would be nice for other functions as "memset" too.

    Thank you already for your answering and advices.

    Joke.
    (BTW the name Joke is no joke . It is very common Dutch girls name and is pronounced as "jo-ke" and not as the English word joke).

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    strncpy( regel2, &regel1[10], 3 );
    You were pretty close.

    BTW, strncpy doesn't append a \0 (like strcpy does), so you have to do that yourself.
    regel2[3] = '\0';
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Not sure of your intent on using

    Code:
    ptr = &regel1[10];
    Remove that and simply call

    Code:
    strncpy(regel2, regel1, 3);
    Should do the trick

  4. #4
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Sorry, noticed my flaw. Just change your ptr* to simply be ptr. Overlooked that you wanted to start at position 10 of your string array.

    Code:
    strncpy(regel2, ptr, 3);

    Also

    If the array pointed to by s2 is a string that is shorter than n bytes, null bytes are appended to the copy in the array pointed to by s1, until n bytes in all are written.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. why is strncpy segfaulting here?
    By Calef13 in forum C Programming
    Replies: 3
    Last Post: 12-29-2008, 03:27 PM
  2. strncpy definition
    By abc123 in forum C Programming
    Replies: 6
    Last Post: 11-30-2006, 03:17 AM
  3. strncpy question, size -1??
    By fayte in forum C Programming
    Replies: 16
    Last Post: 03-16-2006, 11:32 PM
  4. file saving: trouble with strings and strncpy()
    By psychopath in forum C++ Programming
    Replies: 10
    Last Post: 09-17-2005, 10:26 PM
  5. strncpy doesn't seem to work
    By linucksrox in forum C++ Programming
    Replies: 3
    Last Post: 09-08-2005, 01:34 PM

Tags for this Thread