• 《深入理解C指针》第四章 指针和数组


    2019-12-01

    19:07:20

     

     

     

    #include <bits/stdc++.h>
    #include <stdlib.h>
    #include <stdio.h>
    using namespace std;
    #define maxn 10005
    #define M 105
    char *getLine(void){
        const size_t sizeIncrement = 10;
        char* buffer = (char*)malloc(sizeIncrement);
        printf("%d
    ",buffer);
        char* currentPosition = buffer;
        size_t maximumLength = sizeIncrement;
        size_t length = 0;
        int character;
        if(currentPosition ==NULL){
            return NULL;
        }
        while(1){
            character = fgetc(stdin);
            if(character == '
    '){
                break;
            }
            if(++length >= maximumLength){
                char *newBuffer = (char*)realloc(buffer,maximumLength += sizeIncrement);
                printf("%d
    ",newBuffer);
                if(newBuffer == NULL){
                    free(buffer);
                    return NULL;
                }
                currentPosition = newBuffer +(currentPosition - buffer);
                buffer = newBuffer;
            }
            *currentPosition++ = character;
            printf("%d
    ",currentPosition);
        }
        *currentPosition = '';
        return buffer;
    }
    int main(){
        getLine();
        system("pause");
        return 0;
    }

     可以看出realloc后返回的还是一开始的地址:10882360

     

     

     

     

     

     

     

     

     

    #include <bits/stdc++.h>
    #include <stdlib.h>
    #include <stdio.h>
    using namespace std;
    #define maxn 10005
    #define M 105
    
    int main(){
        int* arr[5];
        for(int i=0;i<5;++i){
            *(arr+i) = (int*)malloc(sizeof(int));
            **(arr+i) = i;
            printf("%d
    ",**(arr+i));
        }
        system("pause");
        return 0;
    } 


     

    #include <bits/stdc++.h>
    #include <stdlib.h>
    #include <stdio.h>
    using namespace std;
    #define maxn 10005
    #define M 105
    
    int main(){
        int matrix[2][5] = {{1,2,3,4,5},{6,7,8,9,10}};
        for(int i=0;i<2;++i){
            for(int j=0;j<5;++j){
                printf("matrix[%d][%d] Address: %p Value: %d
    ",
                i,j,&matrix[i][j],matrix[i][j]);
            }
        }
        system("pause");
        return 0;
    } 

     

     

     

     

     

     

    #include <bits/stdc++.h>
    #include <stdlib.h>
    #include <stdio.h>
    using namespace std;
    #define maxn 10005
    #define M 105
    
    int main(){
        char command[16];
        printf("Enter a Command: ");
        scanf("%s",command);
        if(strcmp(command,"Quit")==0){
            printf("The command was Quit");
        }else{
            printf("The command was not Quit");
        }
        system("pause");
        return 0;
    } 

     

     

     

     

     

     

    #include <bits/stdc++.h>
    #include <stdlib.h>
    #include <stdio.h>
    using namespace std;
    #define maxn 10005
    #define M 105
    
    int main(){
        char* error = "ERROR: ";
        char* errorMessage = "Not enought memory";
        char* buffer = (char*)malloc(strlen(error)+strlen(errorMessage)+1);
        strcpy(buffer,error);
        strcat(buffer,errorMessage);
        printf("%s
    ",buffer);
        printf("%s
    ",error);
        printf("%s
    ",errorMessage);
        system("pause");
        return 0;
    } 

     

     

  • 相关阅读:
    git项目管理-合并请求
    记录一次git stash找回删除的存储
    chrome 下 position:fixed失效(react)
    css3 var变量
    rc-select下拉选择控件库推荐
    (转载)vue路径后面去除#号
    本地配置独立域名环境
    javascript判断pc还是手机端
    javascript复制到粘贴板的方案
    javascript轮播插件的使用(TouchSlide)
  • 原文地址:https://www.cnblogs.com/JasonPeng1/p/11967221.html
Copyright © 2020-2023  润新知