可以在字符数组可以与任何数据类型使用吗? [英] Can a char array be used with any data type?

查看:209
本文介绍了可以在字符数组可以与任何数据类型使用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的malloc()函数返回类型的指针无效* 。它根据作为参数传递给它的为size_t 值以字节为单位分配内存。由此产生的分配是可以与任何数据类型C中使用的(不含铸造)原始字节。

这是返回一个函数内可以与类型的数组字符声明无效* ,与任何数据类型一起使用像产生的的malloc

分配

例如,

 的#include<&stdio.h中GT;无效* Stat_Mem();INT主要(无效)
{
    //尺寸:10 * sizeof的(INT)
    INT BUF [] = {1,2,3,4,5,6,7,8,9,10};    为int * p = Stat_Mem();    的memcpy(P,BUF,sizeof的(BUF));    为(中间体n = 0时; N小于10; N ++){
        的printf(%d个P [N]);
    }
    的putchar('\\ n');    返回0;
}无效* Stat_Mem()
{
    静态字符数组[128];
    返回数组;
}


解决方案

声明类型的静态对象阵列字符。有效类型此对象是它的声明类型。有效类型的静态对象不能改变,从而为计划的其余部分有效键入阵列字符

如果你尝试用一个类型,不兼容访问对象的值,或不在此列表中 1 ,其行为是不确定的。

您code试图访问的存储值阵列使用类型 INT 。此类型与类型兼容字符,而不是例外列表中,这样当你阅读使用阵列的行为是不确定的 INT 指针 p

 的printf(%d个P [N]);


1 (引自:ISO:IEC 9899:201X 6.5防爆pressions 7)结果
一个对象应具有其存储的值由左值仅访问
 具有下列类型之一前pression:搜索
 - 一种
 与有效类型的物体,结果,兼容
  - 一个合格
 与有效类型的对象,兼容的类型的版本搜索
  - 这是在签订了型或无符号类型对应于
 有效的对象类型,结果的
  - 一种类型,它是符号或无符号
 键入对应于有效类型的的一个合格的版本
 对象,结果
- 这包括一个聚合或联合类型
 上述类型的成员(包括递归,一个中
 一子聚集的成员或包含的联合),或结果
- 字符类型

The malloc() function returns a pointer of type void*. It allocates memory in bytes according to the size_t value passed as argument to it. The resulting allocation is raw bytes which can be used with any data type in C(without casting).

Can an array with type char declared within a function that returns void *, be used with any data type like the resulting allocation of malloc?

For example,

#include <stdio.h>

void *Stat_Mem();

int main(void)
{
    //size : 10 * sizeof(int)
    int buf[] = { 1,2,3,4,5,6,7,8,9,10 };

    int *p = Stat_Mem();

    memcpy(p, buf, sizeof(buf));

    for (int n = 0; n < 10; n++) {
        printf("%d ", p[n]);
    }
    putchar('\n');

    return 0;
}

void *Stat_Mem()
{
    static char Array[128];
    return Array;
}

解决方案

The declared type of the static object Array is char. The effective type of this object is it's declared type. The effective type of a static object cannot be changed, thus for the remainder of the program the effective type of Array is char.

If you try to access the value of an object with a type that is not compatible with, or not on this list1, the behavior is undefined.

Your code tries to access the stored value of Array using the type int. This type is not compatible with the type char and is not on the list of exceptions, so the behavior is undefined when you read the array using the int pointer p:

printf("%d ", p[n]);


1 (Quoted from: ISO:IEC 9899:201X 6.5 Expressions 7 )
An object shall have its stored value accessed only by an lvalue expression that has one of the following types:
— a type compatible with the effective type of the object,
— a qualified version of a type compatible with the effective type of the object,
— a type that is the signed or unsigned type corresponding to the effective type of the object,
— a type that is the signed or unsigned type corresponding to a qualified version of the effective type of the object,
— an aggregate or union type that includes one of the aforementioned types among its members (including, recursively, a member of a subaggregate or contained union), or
— a character type.

这篇关于可以在字符数组可以与任何数据类型使用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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