C++Primer Plus中指针与字符串例程VS2017版

#include "pch.h"
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
#include <cstring>
//#include <climits>
using namespace std;

int main() {
    char animal[20] = "bear";
    const char* bird = "wren";
    char* ps;
    cout << animal << " and ";
    cout << bird << ‘\n‘;
    cout << "Enter a kind of animal: ";
    cin >> animal;
    ps = animal;
    cout << ps << "!\n";
    cout << "Before using strcpy():\n";
    cout << animal << " at " << (int *)animal << endl;
    cout << ps << " at " << (int*)ps << endl;
    ps = new char[strlen(animal) + 1];
    strcpy_s(ps, strlen(animal) + 1,animal);
    cout << "After using strcpy():\n";
    cout << animal << " at " << (int*)animal << endl;
    cout << ps << " at " << (int*)ps << endl;
    delete [] ps;
    return 0;
}

书籍中给出的源码在VS2017环境下编译会报错,因为VS2017需要使用strcpy()的安全版本strcpy_s(),然而当我们直接替换时,编译器仍然会报错,这是因为strcpy_s()中的参数与strcpy并不完全相同,需要保证缓冲区,直接new的话仍然会报错,需要在strcpy_s()中第二个参数加入被cpy参数的长度。修改完成再编译即可通过。

原文地址:https://www.cnblogs.com/lightmonster/p/10279663.html

时间: 2024-08-06 15:58:43

C++Primer Plus中指针与字符串例程VS2017版的相关文章

C语言中指针和字符串中的一些小结

<pre name="code" class="objc"><span style="font-family: Arial, Helvetica, sans-serif;">void a_A(char *str){</span> for (int i =0; *(str+i) != '\0'; i++) { if (*(str+i) >= 'a' && *(str+i) <= 'z'

C语言中指针定义的字符串和数组定义的字符串的区别

2016-12-03 数组定义字符串: 每次定义数组的时候,系统都会在内存开辟你指定数组大小的空间,并且数组中的内容对于我们是可读可写的,看如下代码: 1 #include<stdio.h> 2 int main() 3 { 4 char str[100] = "hello world"; 5 char str1[100] = "hello world"; 6 str[0] = 'd'; 7 printf("%s\n\n",str);

结构体指针内存——指针数组——字符串指针内存申请

前几天用的结构体,结构体内还包含有结构体指针和数组以及指向字符串的指针,发现自己对这方面的东西还很容易犯错,故现在讲其中容易出错的地方写出来,分享给大家也方便自己日后查看. typedef struct { char name[50]; char job[50]; int age; int people_id; } peopleInfo; typedef struct { bool typeAdd; bool typeDel; int length; peopleInfo *info; char

深入理解C指针之五:指针和字符串

基础概念 字符串可以分配到内存的不同区域,通常使用指针来支持字符串操作.字符串是以ASCII字符NUL结尾的字符序列.ASCII字符NUL表示为\0.字符串通常存储在数组或者从堆上分配的内存中.不过,并非所有的字符数组都是字符串.例如,字符数组可能没有NUL字符. C中有两种类型的字符串. * 单字节字符串.由char数据类型组成的序列. * 宽字符串.由wchar_t数据类型组成的序列. wchar_t数据类型用来表示宽字符串,可能是16位或32位宽.这两种字符串都以NUL结尾.宽字符主要用来

地址/指针和字符串

今天做作业时,发现下列一个问题. 首页是这个自编的strncpy函数: #include "ctype.h" #include "stdlib.h" #include "string.h" #include "windows.h" int main() { char *strata(char s1[],char s2[],int n); char nam1[41]="das"; char nam2[41]=

指针 指针与数组 指针与字符串 指针与函数 结构体与指针 宏

指针 指针与数组 指针与字符串 指针与函数?? 指针与数组 1.数组名:数组元素首地址 eg: int array[3]={1,3,6}; 这里array 恒等于&array[0] 2.int *p = a; int *p = 0; int a[]={0}; 3.int *p = a; 均指向数组的首地址 *p是? *(p+1)是?(*p)+1是? *(p+1)决定向下移动几个字节是类型 4.指针可以当数组名使用 p[1] = 3; 恒等于a[1] ;恒等于*(p+1);恒等于*(a+1) 5.

指针与字符串实例练习

在C语言中,字符串是存放在字符数组中的,字符串的引用如下: char string[] ="I love China!";//定义字符数组string printf("%s\n",string);//输出整个字符串 printf("%c\n",string[7]);//输出第七个元素 该字符串长度是14,最后一个字节存放字符串结束符'\0'. 例1:通过字符指针变量输出一个字符串. 1 int main() 2 { 3 char *string=

浅谈运用指针引用字符串

一.字符串的引用方式 1.如果字符串存放在字符数组中可以用数组名和下标引用 char string[10] = "Hello word"; printf("%s",string); 2.用字符指针变量指向字符串常量,用字符指针引用字符串 char *strp = "Hello word"; printf("%s",strp); //系统将字符串常量以字符数组形式保存在内存中,字符串末尾系统自动加了一个'\0'.再将字符数组的首

C++中,申请字符串数组可用new实现

C++中,申请字符串数组可用new实现: char ** list = new char*[MAX_NUM]; for (int i = 0; i< MAX_LOOP; i++) list[i] = new char[STR_LEN]; 当然也可以不用上面的for()循环,在指定下标的 list[] 分配指定长度的char数组.例如: char a[100]; -- int str_len = strlen(a); list[id] = new char[str_len+1]; strncpy(