指向指针的地址分配不起作用 [英] Assigment of adress to pointer doesn't work

查看:57
本文介绍了指向指针的地址分配不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

words_ptr [i] =& words [i]不起作用.
它说它不能从"char(* _ w64)[10]转换为" char"

The words_ptr[i] = &words[i] doesn''t work.
It says that it cannot convert from ''char(*_w64)[10] to ''char''

#define NUM_STRINGS 10
#define STR_LEN 10
int main (void) {
    char words[NUM_STRINGS][STR_LEN];
    char input[STR_LEN];
    char *words_ptr[NUM_STRINGS];
    char *temp;
    int i, j;
    int count = 0;
    for (i = 0; i < NUM_STRINGS; ++i) {
        printf("Enter a word (or 0 to quit): ");
        scanf ("%9s", input);
        discard_input();
        if (input[0] == '0') break;
        strncpy(words[i], input, STR_LEN);
        words_ptr[i] = &words[i];  //mistake
        ++count;
    }

推荐答案

您不需要&"号:words是一个二维数组,因此其行为很像指针数组.
You do not need an ampersand: words is a two-dimensional array, so it behaves a lot like an array of pointers.


正确!您不能这样分配.
但是您可以在分配指针之前先对其进行强制转换.
True! You can''t assign it that way.
But you can cast the pointer before assigning it.
words_ptr[i] = (char*)&words[i]


这篇关于指向指针的地址分配不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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