zoukankan      html  css  js  c++  java
  • 《深入理解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;
    } 

     

     

  • 相关阅读:
    HTML5新特性之离线缓存技术
    摘要
    典藏百度前端面试题
    idea连接mysql数据库
    报错:[stack Error: Can't find Python executable "python"] vue项目npm install
    SVN客户端(小乌龟)checkout(检出)文件(项目)到本地
    java日期Date工具类 日期格式转换
    radio标签 onchange事件
    js 写带有返回值的function遇到的返回值不正常的情况
    读取zip包内根目录文件的文件名
  • 原文地址:https://www.cnblogs.com/JasonPeng1/p/11967221.html
Copyright © 2011-2022 走看看