简体   繁体   English

如何将字符串分配给char *指针?

[英]How to assign string into char* pointer?

#include <stdio.h>

struct Analysis {
    int lnlen;   
    int arr[2]; 
    char* name;
};

int main()
{
    struct Analysis ana_space[2];
    char *ptr = (void*) &ana_space;

    ana_space[0].lnlen = 0;
    ana_space[0].arr[0] = 1;
    ana_space[0].arr[1] = 2;
    ana_space[0].name = "Peter";

    printf("\n%d\n", *ptr);     // print 0;

    *ptr = 10;                  // how to use memcpy here;

    printf("\n%d\n", *ptr);     // print 10;

    ptr = ptr + sizeof(int);    // advance pointer by int;

    printf("\n%d\n", *ptr);     // print 1;  

    ptr = ptr + 2*sizeof(int);  // advance pointer by 2 ints;

    printf("\n%s\n", *ptr);     // print "Peter"; --------------not work

    //*ptr = "Jim";             // how to assign new name "Jim" into that memory;
    return 0;
}

Output: 输出:

0 0

10 10

1 1

(null) (空值)

I want to use char * as pointer to go through memory address to get some data and also store value into memory. 我想使用char *作为指针来遍历内存地址以获取一些数据并将值存储到内存中。

For int and int array, it works fine, but not for the string. 对于int和int数组,它可以正常工作,但不适用于字符串。

How to print the string and store new string value into memory? 如何打印字符串并将新的字符串值存储到内存中?

The code you presented could cause undefined behavior due to padding and representations of types, which are implementation-defined. 您呈现的代码可能由于填充和类型的表示而导致未定义的行为,而填充和类型表示是实现定义的。

After you increment the pointer ptr here: 在这里增加指针ptr之后:

ptr = ptr + 2*sizeof(int);

the pointer ptr points to the member name of the struct Analysis. 指针ptr指向结构Analysis的成员名称。 If you dereference the pointer ptr, you get the type char and thus a single byte. 如果取消引用指针ptr,则将得到char类型,从而得到一个字节。 This byte does not represent a pointer to the string. 该字节不代表该字符串的指针。

The pointer ptr will have to be cast to the type pointer to a pointer to char, and then dereferenced so the correct and full value of the member name will be obtained. 指针ptr必须转换为指向char指针的类型指针,然后再取消引用,以便获得成员名称的正确和完整的值。

That resulting value is a pointer to the string "Peter" . 该结果值是指向字符串"Peter"的指针。

ANSI C has a macro called offsetof() found in stddef.h that gives a more sure way of calculating the pointer offset of a member within a struct. ANSI C在stddef.h中有一个名为offsetof()的宏,它提供了一种更可靠的方法来计算结构中成员的指针偏移量。 Here, we can get the the address of the name member in ana_space[0] directly. 在这里,我们可以直接在ana_space [0]中获取名称成员的地址。

ptr = (char*) &ana_space + offsetof(struct Analysis, name);

This takes out any guess work on padding. 这样就排除了关于填充的任何猜测工作。

This pointer then has to be properly cast to print the contents of name: 然后必须正确地转换此指针以打印名称的内容:

printf("%s\n", *(char**) ptr);

Your approach is not portable. 您的方法不可移植。 It will be better to use offsetof to make sure that you can reliably point to the addresses of the members of a struct . 最好使用offsetof来确保可以可靠地指向struct成员的地址。

int main()
{
    struct Analysis ana_space[2];
    char *ptr = (void*) &ana_space;

    size_t offset1 = offsetof(struct Analysis, arr);
    size_t offset2 = offsetof(struct Analysis, name);

    ana_space[0].lnlen = 0;
    ana_space[0].arr[0] = 1;
    ana_space[0].arr[1] = 2;
    ana_space[0].name = "Peter";

    // advance pointer to point to arr.
    ptr = ptr + offset1;

    // advance pointer to point to name
    ptr = ptr + (offset2-offset1);

    // Cast the pointer appropriately before dereferencing.
    printf("\n%s\n", *(char**)ptr); 

    // how to assign new name "Jim" into that memory;
    *(char**)ptr = "Jim";
    printf("\n%s\n", *(char**)ptr);

    return 0;
}

Your use of: 您的使用:

printf("\n%d\n", *ptr);     // print 0;

*ptr = 10;                  // how to use memcpy here;

printf("\n%d\n", *ptr);     // print 10;

and the expected output is flawed. 并且预期的输出是有缺陷的。 It works only with little endian systems. 它仅适用于小端序系统。 I suggest using: 我建议使用:

printf("\n%d\n", *(int*)ptr);

*(int*)ptr = 10;

printf("\n%d\n", *(int*)ptr);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM