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;
    } 

     

     

  • 相关阅读:
    tomcat安装
    卸载重安firefox
    Metasploit笔记之信息收集命令
    postgresql-9.0.18-1-linux.run启动
    ubuntu 安装自启动管理
    MySQL数据库”mysql SQL Error:1146,SQLState:42S02 “解决方法
    PE笔记之节表
    标准类型String(学习中)
    链表实现(打印元素的实现)
    C++中new和delete来创建和释放动态数组
  • 原文地址:https://www.cnblogs.com/JasonPeng1/p/11967221.html
Copyright © 2011-2022 走看看