使用指针的C函数变化串 [英] C function change string using pointer

查看:72
本文介绍了使用指针的C函数变化串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做出改变,从主要功能的字符数组的函数,这就是我想要做的:

 的#include<&stdlib.h中GT;
#包括LT&;&stdio.h中GT;
#包括LT&;&CONIO.H GT;无效的变化(字符*一);诠释的main()
{
    所以char a [] =你好;
    的printf(\\ NA =%s的,一);
    变化(一);
    的printf(%\\ NA =%s的,一);
    残培();
}无效的变化(字符*一)
{
    A =再见;
}


解决方案

若干问题与此code,但首先我们需要后退一步,谈谈阵列如何用C来处理。

除非它是的sizeof 或一元&放大器的操作; 运营商,或者是一个字符串使用初始化在声明中另一个数组,键入 T N个元素的数组的前pression将被转换(衰变),以一个前$ p类型$ pssion指针 T ,和前pression的价值将是数组的第一个元素的地址。

在声明

 的char a [] =你好;

你好是一个字符串字面量,其类型为字符 6个元素的数组(5字符加上0终结)。因为它是被用来初始化数组 A 在声明,上面的规则不适用;相反,大小 A 被设定为相同的文字(6),以及字符串的内容被复制到阵列的大小。

当你调用更改

 的变化(一);

前pression A 有类型的第6个元素的数组字符。既然既不是字符串,也不是的sizeof 或一元&放大器的操作; 运营商,即前pression将被转换为类型指针字符,和前pression的价值将是阿雷的第一个元素的地址。因此,为什么变更函数声明为

 无效的变化(字符*一);

在此背景下, A 只是一个指针。当你写

  A =再见;

字符串文字再见在初始化中被使用,并且它不是的sizeof 或一元&安培; 运营商,所以前pression转换为类型指针字符,和前pression的值是第一个字符的地址。所以,这里所发生的是,你复制的地址的字符串文字再见 A 。这将覆盖值 A ,但这 A 是不是在内存中的阵列相同的对象 A ,所以它的任何变化都不会反映在

如果您想更新一个数组的内容的,则需要使用库函数的strcpy /函数strncpy (为0终结字符串)或的memcpy (适用于一切),或明确更新每个元素( A [0] ='G';一个[1] =' O';一个[2] ='O'; 等)。

要更新 A 的内容,你会用

 的strcpy(一,再见);

除了 ...

A 只是大到足以容纳5个字符加一个0终止; 再见是7个字符加上0终结;它比 A 能够存储较大的两个字符。按c happliy让您执行操作和垃圾字节紧随 A ,这可能会导致许多问题(缓冲区溢出,如这是一个典型的恶意软件攻击)。你有两个选择在这个时刻:

首先,你可以声明 A 要足够大,以处理任何字符串:

 的#define MAX_LEN 10
...
所以char a [MAX_LEN] =你好;

第二,你可以限制复制到字符串的大小 A

 无效的变化(字符*一,为size_t大小)
{
  函数strncpy(一,再见,大小 - 1);
  一个[大小 - 1] = 0;
}

请注意,你将需要传递的元素数 A 可存储当你调用一个单独的参数更改;有没有办法从一个指针来告诉数组它有多大指出的是:

 变化(A,sizeof的A / * sizeof的一); //虽然在这种情况下,与sizeof一个将是
                                   //足够了。

I'm trying to make a function that changes a char array from the main function, that's what I'm trying to do:

#include <stdlib.h>
#include <stdio.h>
#include <conio.h>

void change(char *a);

int main()
{
    char a[] = "hello";
    printf("\na = %s", a);
    change(a);
    printf("%\na = %s", a);
    getch();
}

void change(char *a)
{
    a = "goodbye";
}

解决方案

Several problems with this code, but first we need to take a step back and talk about how arrays are handled in C.

Except when it is the operand of the sizeof or unary & operators, or is a string literal used to initialize another array in a declaration, an expression of type "N-element array of T" will be converted ("decay") to an expression of type "pointer to T", and the value of the expression will be the address of the first element of the array.

In the declaration

char a[] = "hello";

"hello" is a string literal, which has type "6-element array of char" (5 characters plus the 0 terminator). Since it is being used to initialize the array a in a declaration, the rule above doesn't apply; instead, the size of a is set to be the same as the size of the literal (6), and the contents of the string literal are copied to the array.

When you call change from main as

change(a);

the expression a has type "6-element array of char". Since it is neither a string literal nor the operand of the sizeof or unary & operators, that expression will be converted to type "pointer to char", and the value of the expression will be the address of the first element of the aray. Hence why the change function is declared as

void change(char *a);

In this context, a is simply a pointer. When you write

a = "goodbye";

the string literal "goodbye" is not being used in an initializer, and it's not the operand of the sizeof or unary & operators, so the expression is converted to type "pointer to char", and the value of the expression is the address of the first character. So what happens here is that you're copying the address of the string literal "goodbye" to a. This overwrites the value in a, but this a is not the same object in memory as the array a in main, so any changes to it are not reflected in main.

If you want to update the contents of an array, you will need to use the library functions strcpy/strncpy (for 0-terminated strings) or memcpy (for everything else), or update each element explicitly (a[0]='g'; a[1]='o'; a[2]='o';, etc).

To update the contents of a, you'd use

strcpy( a, "goodbye" );

Except...

a is only large enough to hold 5 characters plus a 0 terminator; "goodbye" is 7 characters plus the 0 terminator; it's two characters larger than what a is capable of storing. C will happliy let you perform the operation and trash the bytes immediately following a, which may lead to any number of problems (buffer overruns such as this are a classic malware exploit). You have a couple of choices at this juncture:

First, you could declare a to be large enough to handle either string:

#define MAX_LEN 10
...
char a[MAX_LEN] = "hello";

Second, you could limit the size of the string copied to a:

void change( char *a, size_t size )
{
  strncpy( a, "goodbye", size - 1 );
  a[size - 1] = 0;
}

Note that you will need to pass the number of elements a can store as a separate parameter when you call change; there's no way to tell from a pointer how big the array it points to is:

change( a, sizeof a / sizeof *a ); // although in this case, sizeof a would be
                                   // sufficient.

这篇关于使用指针的C函数变化串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆