怎么用指针改字符数组呀?

img

因为数组传入的是首元素地址,所以我们一般采用循环来用指针改数组,但注意不要改过头,不让会变成野指针。

(写得有些匆忙,若有不足之处,敬请留言指正)

#include<stdio.h>
#include<string.h>
int main(){
    char str[] = "helloworld";
    char *p = str;
    puts(gets(p));
    puts(str); 
}


#include<stdio.h>
#include<string.h>
using namespace std;

int main()
{
    char str[] = "helloworld";
    char *p = str;
    const char *s = "heabcdorld";
    for(int i=0;s[i]!='\0';i++)
    {
        p[i] = s[i];
    }
    cout<<p<<endl;

}