Sponsored Content
Full Discussion: strncpy(ver, buffer2 +5,2);
Top Forums Programming strncpy(ver, buffer2 +5,2); Post 83302 by Corona688 on Tuesday 13th of September 2005 10:00:46 PM
Old 09-13-2005
Quote:
Originally Posted by whatisthis
Hi,
Can someone help me understand this code?
If we start with this:
Code:
char buffer2[]="abcdeFGH";
char job[6];
char ver[3];

then, strncpy(job, buffer2,5); will copy the first 5 characters of buffer2 into job, i.e. "abcde". It will NOT null-terminate it, though. strncpy doesn't NULL-terminate the destination string unless it actually finds the NULL terminator.

Note that buffer2 is an array, and adding an offset to an array moves you farther down that array. So buffer2+5 moves it 5 down, to the"FGH" part. So, strncpy(ver, buffer2 +5,2); will copy 2 characters of "FGH" into ver, but again, doesn't NULL terminate it because it didn't find the NULL terminator.
 

9 More Discussions You Might Find Interesting

1. Cybersecurity

BIND Ver. 8

Hi everyone I'm a HPUX system admin, and need some help. Our organisation is full of red tape and managers like that like to hold many meetings but take little action. I need to persuade my line manager that we should move our two nameservers onto BIND 8. Can anyone please point me to some... (2 Replies)
Discussion started by: alwayslearningunix
2 Replies

2. AIX

New IBM AIX 5L ver 5.1 .. any thoughts?

I read that IBM came up with a new AIX version that would be fully integrated with Linux (I guess compared to older versions of AIX that could not) and I was hoping I bring this for discussion here so I learn more about IBM UNIX OS Can anybody put few simple and practical comparisons between... (0 Replies)
Discussion started by: aliissa
0 Replies

3. UNIX for Advanced & Expert Users

X25 on Tru UNIX ver 5

How do i set up X25 protocol on version 5 of Tru Unix (Compaq). I have manged to do it using version 4. What are the major differences.:confused: (1 Reply)
Discussion started by: samudimu
1 Replies

4. Programming

Strncpy - coredump

haiu all what makes strncpy to coredump (0 Replies)
Discussion started by: vijaysabari
0 Replies

5. UNIX for Dummies Questions & Answers

USB support in what ver. HP-UX

Can someone clue me in as to what version of HP-UX did USB support start? Specifically mass storage support. We have serveral HP Visualize systems and would like to connect up an external USB drive to do backups. (0 Replies)
Discussion started by: emcatc
0 Replies

6. Programming

Reg: strncpy function

Hi All, Please clear the following doubt. int main() { char str1; char str2 = "HelloWorld"; int len = strlen(str2); strncpy(str1, str2, len); str1 = '\0'; printf("%s", str1); } In the above program i'm initializing the 10th byte of str1 to null. what will happen to the memory of... (13 Replies)
Discussion started by: arunviswanath
13 Replies

7. Solaris

SOLARIS/BEA WEBLOGIC ver.8.1

Hello all, I am having a little issue with BEA webloigc server Version 8.1, previously, we we confugred we where able to Grep processes for BEA and we could get the processes listed now I am only able to see the start weblogic script process. the weblogic server is up and running and performing its... (2 Replies)
Discussion started by: jvgvr6
2 Replies

8. UNIX for Dummies Questions & Answers

Question on Redhat ver.6 RPM's

Hi, Using Redhat version 6.0, I need nmap so I have downloaded. But it requires many other dependencies, when I try to install it. See below err.msg. Is there any other way to get this installed, or I need to download each of the dependencies. rpm -Uhv nmap-5.51-2-mdv2011.0.x86_64.rpm... (3 Replies)
Discussion started by: samnyc
3 Replies

9. HP-UX

GCC ver 3.4.6

anyone has an idea from where can i download gcc 3.4.6 for hp-ux 11iv3 itanium? (0 Replies)
Discussion started by: guy3145
0 Replies
strncpy(9r)															       strncpy(9r)

NAME
strncpy - General: Copies a null-terminated character string with a specified limit SYNOPSIS
char * strncpy( char *s1, char *s2, int n ); ARGUMENTS
Specifies a pointer to a buffer of at least n bytes. Specifies a pointer to a string (an array of characters terminated by a null charac- ter). Specifies the number of characters to be copied. DESCRIPTION
The strncpy routine copies string s2 to buffer s1. The routine stops copying after it copies a null character or n characters, whichever comes first. If the length of s2 as determined by the null character is less than n, the routine pads s1 with null characters. RETURN VALUES
The strncpy routine returns a pointer to /NULL at the end of the first string (or to the location following the last copied character if there is no NULL). The copied string will not be null terminated if the length of s2 is n characters or more. SEE ALSO
Routines: bcopy(9r), blkclr(9r), copystr(9r), ovbcopy(9r), strcpy(9r) strncpy(9r)
All times are GMT -4. The time now is 09:59 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy