strncpy, _strncpy_l, wcsncpy, _wcsncpy_l, _mbsncpy, _mbsncpy_l

Copiare i caratteri di una stringa in un'altra. Sono disponibili versioni più sicure di queste funzioni; vedere strncpy_s, _strncpy_s_l, wcsncpy_s_wcsncpy_s_l, , _mbsncpy_s, . _mbsncpy_s_l

Importante

_mbsncpy e _mbsncpy_l non possono essere usati nelle applicazioni eseguite in Windows Runtime. Per altre informazioni, vedere Funzioni CRT non supportate nelle app della piattaforma UWP (Universal Windows Platform).

Sintassi

char *strncpy(
   char *strDest,
   const char *strSource,
   size_t count
);
char *_strncpy_l(
   char *strDest,
   const char *strSource,
   size_t count,
   _locale_t locale
);
wchar_t *wcsncpy(
   wchar_t *strDest,
   const wchar_t *strSource,
   size_t count
);
wchar_t *_wcsncpy_l(
   wchar_t *strDest,
   const wchar_t *strSource,
   size_t count,
   _locale_t locale
);
unsigned char *_mbsncpy(
   unsigned char *strDest,
   const unsigned char *strSource,
   size_t count
);
unsigned char *_mbsncpy_l(
   unsigned char *strDest,
   const unsigned char *strSource,
   size_t count,
   _locale_t locale
);
template <size_t size>
char *strncpy(
   char (&strDest)[size],
   const char *strSource,
   size_t count
); // C++ only
template <size_t size>
char *_strncpy_l(
   char (&strDest)[size],
   const char *strSource,
   size_t count,
   _locale_t locale
); // C++ only
template <size_t size>
wchar_t *wcsncpy(
   wchar_t (&strDest)[size],
   const wchar_t *strSource,
   size_t count
); // C++ only
template <size_t size>
wchar_t *_wcsncpy_l(
   wchar_t (&strDest)[size],
   const wchar_t *strSource,
   size_t count,
   _locale_t locale
); // C++ only
template <size_t size>
unsigned char *_mbsncpy(
   unsigned char (&strDest)[size],
   const unsigned char *strSource,
   size_t count
); // C++ only
template <size_t size>
unsigned char *_mbsncpy_l(
   unsigned char (&strDest)[size],
   const unsigned char *strSource,
   size_t count,
   _locale_t locale
); // C++ only

Parametri

strDest
Stringa di destinazione.

strSource
Stringa di origine.

count
Il numero dei caratteri da copiare.

locale
Impostazioni locali da usare.

Valore restituito

Restituisce strDest. Nessun valore restituito è riservato per indicare un errore.

Osservazioni:

La funzione strncpy copia i caratteri iniziali count di strSource in strDest e restituisce strDest. Se count è minore o uguale alla lunghezza di strSource, un carattere Null non viene aggiunto automaticamente alla stringa copiata. Se count è maggiore della lunghezza di strSource, alla stringa di destinazione vengono aggiunti caratteri null fino alla lunghezza count. Se le stringhe di origine e di destinazione si sovrappongono, il comportamento di strncpy non è definito.

Importante

strncpy non verifica la presenza di spazio sufficiente in strDest; è pertanto una causa possibile dei sovraccarichi del buffer. L'argomento count limita il numero di caratteri copiato; non è un limite della dimensione di strDest. Vedi l'esempio seguente. Per altre informazioni, vedere Evitare sovraccarichi del buffer.

Se strDest o strSource è un NULL puntatore o se count è minore o uguale a zero, viene richiamato il gestore di parametri non validi, come descritto in Convalida dei parametri. Se l'esecuzione può continuare, queste funzioni restituiscono -1 e impostano errno su EINVAL.

wcsncpy e _mbsncpy sono versioni con caratteri wide e caratteri multibyte di strncpy. Gli argomenti e i valori restituiti da wcsncpy e _mbsncpy variano di conseguenza. In alternativa queste sei funzioni si comportano in modo identico.

Le versioni di queste funzioni che presentano il suffisso _l sono identiche ad eccezione del fatto che, per il comportamento dipendente dalle impostazioni locali, usano le impostazioni locali passate anziché quelle correnti. Per altre informazioni, vedere Locale.

In C++ queste funzioni presentano overload di modello che richiamano le relative controparti più recenti e sicure. Per altre informazioni, vedere Proteggere gli overload dei modelli.

Per impostazione predefinita, lo stato globale di questa funzione è limitato all'applicazione. Per modificare questo comportamento, vedere Stato globale in CRT.

Mapping di routine di testo generico

TCHAR.H Routine _UNICODE e _MBCS non definito _MBCS Definito _UNICODE Definito
_tcsncpy strncpy _mbsnbcpy wcsncpy
_tcsncpy_l _strncpy_l _mbsnbcpy_l _wcsncpy_l

Nota

_strncpy_l e _wcsncpy_l non dipendono dalle impostazioni locali; vengono forniti solo per _tcsncpy_l e non possono essere chiamati direttamente.

Requisiti

Ciclo Intestazione obbligatoria
strncpy <string.h>
wcsncpy <string.h> oppure <wchar.h>
_mbsncpy, _mbsncpy_l <mbstring.h>

Per altre informazioni sulla compatibilità della piattaforma, vedere Compatibilità.

Esempio

Nell'esempio seguente viene illustrato l'utilizzo di strncpy e come l'utilizzo improprio possa causare bug e problemi di sicurezza del programma. Il compilatore genera un avviso per ogni chiamata a simile a strncpycrt_strncpy_x86.c(15) : avviso C4996: 'strncpy': questa funzione o variabile potrebbe non essere sicura. Prendere invece in considerazione l'uso strncpy_s di . Per disabilitare la deprecazione, usare _CRT_SECURE_NO_WARNINGS. Per informazioni dettagliate, vedere la Guida online.

// crt_strncpy_x86.c
// Use this command in an x86 developer command prompt to compile:
// cl /TC /W3 crt_strncpy_x86.c

#include <stdio.h>
#include <string.h>

int main() {
   char t[20];
   char s[20];
   char *p = 0, *q = 0;

   strcpy_s(s, sizeof(s), "AA BB CC");
   // Note: strncpy is deprecated; consider using strncpy_s instead
   strncpy(s, "aa", 2);     // "aa BB CC"         C4996
   strncpy(s + 3, "bb", 2); // "aa bb CC"         C4996
   strncpy(s, "ZZ", 3);     // "ZZ",              C4996
                            // count greater than strSource, null added
   printf("%s\n", s);

   strcpy_s(s, sizeof(s), "AA BB CC");
   p = strstr(s, "BB");
   q = strstr(s, "CC");
   strncpy(s, "aa", p - s - 1);   // "aa BB CC"   C4996
   strncpy(p, "bb", q - p - 1);   // "aa bb CC"   C4996
   strncpy(q, "cc",  q - s);      // "aa bb cc"   C4996
   strncpy(q, "dd", strlen(q));   // "aa bb dd"   C4996
   printf("%s\n", s);

   // some problems with strncpy
   strcpy_s(s, sizeof(s), "test");
   strncpy(t, "this is a very long string", 20 ); // C4996
   // Danger: at this point, t has no terminating null,
   // so the printf continues until it runs into one.
   // In this case, it will print "this is a very long test"
   printf("%s\n", t);

   strcpy_s(t, sizeof(t), "dogs like cats");
   printf("%s\n", t);

   strncpy(t + 10, "to chase cars.", 14); // C4996
   printf("%s\n", t);

   // strncpy has caused a buffer overrun and corrupted string s
   printf("Buffer overrun: s = '%s' (should be 'test')\n", s);
   // Since the stack grows from higher to lower addresses, buffer
   // overruns can corrupt function return addresses on the stack,
   // which can be exploited to run arbitrary code.
}

Output

ZZ
aa bb dd
this is a very long test
dogs like cats
dogs like to chase cars.
Buffer overrun: s = 'ars.' (should be 'test')

Il layout delle variabili automatiche e il livello di rilevamento degli errori e di protezione di codice possono variare con le impostazioni modificate del compilatore. Questo esempio può produrre risultati diversi quando viene compilato in altri ambienti di compilazione o con altre opzioni del compilatore.

Vedi anche

Manipolazione delle stringhe
impostazioni locali
Interpretazione di sequenze di caratteri multibyte
_mbsnbcpy, _mbsnbcpy_l
strcat, wcscat, _mbscat
strcmp, wcscmp, _mbscmp
strcpy, wcscpy, _mbscpy
strncat, _strncat_l, wcsncat, _wcsncat_l, _mbsncat, _mbsncat_l
strncmp, wcsncmp, _mbsncmp, _mbsncmp_l
_strnicmp, _wcsnicmp, _mbsnicmp, _strnicmp_l, _wcsnicmp_l, _mbsnicmp_l
strrchr, wcsrchr, _mbsrchr, _mbsrchr_l
_strset, _strset_l, _wcsset, _wcsset_l, _mbsset, _mbsset_l
strspn, wcsspn, _mbsspn, _mbsspn_l
strncpy_s, _strncpy_s_l, wcsncpy_s, _wcsncpy_s_l, _mbsncpy_s, _mbsncpy_s_l
strcpy_s, wcscpy_s, _mbscpy_s