为什么我不能将值重新分配给指针变量? [英] why can't i reassign values to pointer to pointer variable?

查看:108
本文介绍了为什么我不能将值重新分配给指针变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我感谢您抽出宝贵时间阅读我的问题.
其次,我会说清楚我的话,因为我不会说英语.
请阅读以下程序:

Firstly i thank you for you to spare your precious time to read my question.
Secondly i try to make my words clearly for i am not good at English.
please read the programme below:

#include <stdio.h>

#define CONT 5
#define NUM 5

int main(){
int i,c,d;
char  *ptr[CONT] ={"at","is","he","we","up"};
char  **str = ptr;
for(i = 0;i < NUM; ++i){
        printf("please enter one character at a time\n");
        c = getchar();
        getchar();
        d = getchar();
        getchar();

        **(str + i) = c;       //i want to reassign pointer to pointer variable in this way.
        *(*(str + i) + 1) = d;
        *(*(str + i) + 2) = '\0';

    }
    return 0;
}



我知道还有其他几种方法可以这样做.但是我不明白为什么这种方法行不通.请告诉我哪里出了问题.再次感谢您的帮助.电子邮件:sinalym#163.com
我为str分配了大小,但是下面的代码也不起作用.



i know there are several other ways to do this.But i don''t understand why this way can''t work.please tell me where the wrong is. Thank you for your help again.Email: sinalym#163.com
i mallocate the size for str,but it also doesn''t work.code below.

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

#define CONT 5
#define NUM 3

int main(){

    char **ptr = NULL;
    int c,d;
    //char *str = NULL;
    ptr = (char **)malloc(CONT*sizeof(char *));
    if(ptr == NULL){
        printf("allocate for ptr is failure\n");
        printf("please enter any key to exit\n");
        system("pause");
            exit(1);
    }
    for(int i = 0;i < NUM; ++i){
        *ptr = (char *)malloc(NUM*sizeof(char));

    }
    printf("please enter one character at a time\n");

    for(int i = 0;i < 5;++i){
        c = getchar();
        getchar();
        d = getchar();
        getchar();
        **(ptr + i) = c;
        *(*(ptr + i) + 1) = d;
        *(*(ptr + i) + 2) = '\0';
    }
    for(int i = 0;i < CONT; ++i){
    printf("%c%c  \n",**(ptr +i),*(*(ptr +i) +1));
    }

    for(int i = 0;i < NUM;++i){
        free(ptr[i]);
    }
    free(ptr);

    return 0;
}


我输入每个字符,然后按"ENTER":按"a"并按"ENTER"键,按"b",然后按键"ENTER",按"c"并按键"ENTER".当我输入"d"时,错误再次发生.
mallocDoublePointer.exe中的0x00fd3658处有存在处理的异常:0xC0000005:写入位置0xcdcdcdcd时发生访问冲突
最后一个程序怎么了?


i enter each character then press "ENTER":press ''a'' and press the key "ENTER",press''b'',and press the key "ENTER",press''c''and press the key "ENTER".when i enter ''d'',the wrong occurs again.
mallocDoublePointer.exe 中的 0x00fd3658 处有未经处理的异常: 0xC0000005: 写入位置 0xcdcdcdcd 时发生访问冲突
What''s wrong with the last programme?

推荐答案

这是因为没有这样的英语单词"pertime".您应该写一次一个字符",否则计算机会被冒犯并且拒绝工作:-).使用拼写检查器.

严重的是,您从未为"at",","he","we","up"分配内存的原因.这些值是立即常量,通常放置在严格只读的内存位置.在最典型的实现中,它们与代码放置在同一段中.在大多数计算机中使用的具有Intel MIPS和Pentium体系结构的CPU中,此类内存的可执行代码段使用了针对写访问的硬件保护.

用更简单的代码比较您的操作:
This is because there is no such English word "pertime". You should have written "one character at a time", otherwise computer gets offended and refuses to work :-). Use a spellchecker.

Seriously, the reason that you never allocated memory for "at","is","he","we","up". Those values are immediate constants which are normally placed at a memory location which is strictly read-only. In most typical implementation, they are placed in the same segment as the code. In CPUs with Intel MIPS and Pentium architectures used in most computers, such executable-code segments of memory use hardware protection from write access.

Compare what you do with much simpler code:
char * readOnlyString = "read-only!";

//you expect to get "read-only?"
readOnlyString[9] = '?';
//but no, this code will throw "access violation" exception in most implementations



由于与我上面解释的原因相同,它将引发访问冲突"异常.


CodeProject的一些开发人员正确地指出,如果用户尝试编写上面显示的字符串声明(要求显式使用关键字const),则C ++编译器应更诚实地运行,并显示编译错误.不幸的是,此语言的标准不需要此功能.


—SA



It will throw "access violation" exception by the same reason I explained above.


Some developers at CodeProject rightfully noted that C++ compiler should act more honestly and show a compilation error if a user tries to write the string declaration shown above, requiring explicit use of the keyword const. Unfortunately, such feature is not required by the standards of this language.


—SA


您的第二个代码段中的for循环不正确.应该是:

Your for loop in you second code snippet is incorrect. It should be:

for(int i = 0;i < CONT; ++i){
    *(ptr + i) = (char *)malloc(NUM*sizeof(char));
}



也就是说,您需要进行5次分配而不是3次分配.另外,您需要将分配结果放在适当的位置(而不总是放在第一项中).

您对数组取消分配有相同的错误.由于在CONT上使用NUM代替,因此在循环中分配3项而不是5项.

如果要使用数组符号,则正确处理起来会容易一些.

顺便说一句,使用调试器可以帮助您发现这些错误.



That is, you need to do 5 allocations and not 3. Also, you need to put the result of the allocation at the proper location (and not always in first item).

You have the same error with the array desallocation. You desallocate 3 items instead of 5 in the loop since you uses NUM instead on CONT.

If you would uses array notation, it would be a bit easier to get it right.

By the way, using a debugger would have help you a lot to find these errors.


这篇关于为什么我不能将值重新分配给指针变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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