c: two-dimensional array

/**
 * *****************************************************************************
 * @file        twoDimensional.h
 * @brief       二维数组  Pointers and 2-D arrays
 * @author       geovindu,Geovin Du,涂聚文 ([email protected])
 * ide: vscode c11,c17  windows 10
 * @date        2023-10-30
 * @copyright   geovindu 站在巨人的肩膀上 Standing on the Shoulders of Giants
 * matrix            =>    Points to base address of two-dimensional array.
                           Since array decays to pointer.
 
*(matrix)            =>    Points to first row of two-dimensional array.
*(matrix + 0)        =>    Points to first row of two-dimensional array.
*(matrix + 1)        =>    Points to second row of two-dimensional array.
 
**matrix             =>    Points to matrix[0][0]
*(*(matrix + 0))     =>    Points to matrix[0][0]
*(*(matrix + 0) + 0) =>    Points to matrix[0][0]
*(*matrix + 1)       =>    Points to matrix[0][1]
*(*(matrix + 0) + 1) =>    Points to matrix[0][1]
*(*(matrix + 2) + 2) =>    Points to matrix[2][2]
 * *****************************************************************************
 */
 
#ifndef TWODIMENSIONAL_H_
#define TWODIMENSIONAL_H_
  
#include 
#include 
  
 
#define BUF_LEN 100                    // Length of input buffer
#define COUNT   5                 // Initial number of strings
 /**
 * @brief      输入字符排序
 *
 */
void stringInputSort();
 /**
  * @brief
  *
  * @param arry 二维数组
 * @param row 行长度
 * @param col 列长度
  * @return int
  */
 int pointDisplay(const int** arry,int row,int col);
  
 
  /**
  * @brief      
  * @param arry  二维数组
 * @param row 行长度
 * @param col 列长度
  * @return int
  */
  int pointDisplay1(const** arry,int row,int col);
 /**
 * @brief  
 * @param arry 二维数组
 * @param row 行长度
 * @param col 列长度
 * @return int
 */
 int pointDisplay0(int arry[10][10],int row,int col);
 
/**
 * @brief  OK
 *
 * @param arry 二维数组
 * @param intlength 行列共长度
 * @return int
 */
int pointDisplay2(int arry[10][10],int intlength);
  
  
  
/**
 * @brief
 *
 * @param arry 二维数组
 * @param row 行长度
 * @param col 列长度
 * @return int
 */
int pointDisplay3(int** arry,int row,int col);
  
  
/**
 * @brief       Ok
 * @param arry 二维数组
 * @param row 行长度
 * @param col 列长度
 * @return int
 */
int pointDisplay4(int** arry,int row,int col);
  
  
/**
 * @brief    OK  
 * @param arry 二维数组
  * @param row 行长度
 * @param col 列长度
 * @return int
 */
int pointDisplay5(int*** arry,int row,int col);
 
/**
 * @brief   ok   
 * @param arry 二维数组
 * @param row 行长度
 * @param col 列长度
 * @return int
 */
int pointDisplay6(int** arry,int row,int col);
 
 
/**
 * @brief 释放所有堆内存     
 * @param ps
 * @param n
 *
 */
void freeMemoryChar(char **ps,size_t n);
 
 
 
/**
 * @brief 释放所有堆内存     
 * @param ps
 * @param n
 *
 */
void freeMemoryInt(int **ps,size_t n);
 
 
 
 
  
#endif
/**
 * *****************************************************************************
 * @file        twoDimensional.c
 * @brief       二维数组  Pointers and 2-D arrays
 * @author      geovindu,Geovin Du,涂聚文 ([email protected])
 *  ide: vscode c11,c17  windows 10
 * @date        2023-10-30
 * @copyright   geovindu 站在巨人的肩膀上 Standing on the Shoulders of Giants
 * *****************************************************************************
 */
 
#include 
#include 
#include 
#include 
#include 
#include 
#include "include/twoDimensional.h"
  
 
 
/**
 * @brief      输入字符排序
 *
 */
void stringInputSort()
{
 
    char buf[BUF_LEN];                  // Input buffer
    size_t str_count = 0;               // Current string count
    size_t capacity = COUNT;            // Current maximum number of strings
    char **pS = calloc(capacity, sizeof(char*));    // Pointers to strings
    char** psTemp = NULL;               // Temporary pointer to pointer to char
    char* pTemp = NULL;                 // Temporary pointer to char
    size_t str_len = 0;                 // Length of a string
    bool sorted = false;                // Indicated when strings are sorted
 
    printf("Enter strings to be sorted, one per line. Press Enter to end:\n");
 
    // Read in all the strings
    char *ptr = NULL;
    while(true)
    {
        ptr = fgets(buf, BUF_LEN, stdin);
        if(!ptr)                          // Check for read error
        {
            printf("Error reading string.\n");
            free(pS);
            pS = NULL;
            return 1;
        }
 
        if(*ptr == '\n') break;           // Empty line check
 
        if(str_count == capacity)
        {
            capacity += capacity/4;          // Increase capacity by 25%
 
            if(!(psTemp = realloc(pS, capacity))) return 1;
 
            pS = psTemp;
        }
        str_len = strnlen(buf, BUF_LEN) + 1;  //strnlen_s
        if(!(pS[str_count] = malloc(str_len))) return 2;
        strcpy_s(pS[str_count++], str_len, buf);
    }
 
    // Sort the strings in ascending order
    while(!sorted)
    {
        sorted = true;
        for(size_t i = 0 ; i < str_count - 1 ; ++i)
        {
            if(strcmp(pS[i], pS[i + 1]) > 0)
            {
                sorted = false;               // We were out of order so...
                pTemp= pS[i];                 // swap pointers pS[i]...
                pS[i] = pS[i + 1];            //       and...
                pS[i + 1]  = pTemp;           //     pS[i + 1]
            }
        }
    }
 
    // Output the sorted strings
    printf("Your input sorted in ascending sequence is:\n\n");
    for(size_t i = 0 ; i < str_count ; ++i)
    {
        printf("%s", pS[i] );
        free(pS[i]);                      // Release memory for the word
        pS[i] = NULL;                     // Reset the pointer
    }
    free(pS);                           // Release the memory for pointers
    pS = NULL;                          // Reset the pointer
 
}
 
 /**
  * @brief  可以
  *
  * @param arry  二维数组
 * @param row 行长度
 * @param col 列长度
  * @return int
  */
 int pointDisplay(const** arry,int row,int col)
  {
    //在main 中直接使用可以
    printf("\n6指针遍历二维数组\n");
    int *dup;
    //dup= arry[0];  //*(*(arry + 0));//*(arry + 0);//
    for (int i = 0; i < row; i++) //sizeof(arry) / sizeof(int)
    {
        dup= arry[i];
        for(int j=0;j

调用:

windows 10:

int main()
{
 
    printf("hello c world \n");
    printf("你好,中国\n");
 
     
   // stringInputSort();
 
    int arrdu[5][4]={
        {10,20,30,40},
        {50,60,70,80},
        {90,100,110,120},
        {130,140,150,160},
        {170,180,190,200}       
    };
    // 4 列
    int dum=4;
    //5 行
    int dun=5;
     
 
     for(int i = 0; i 

window10 or Ubuntu 20.4

#include "include/twoDimensional.h"



int main()
{
    printf("hello c world, \n");
    printf("你好,中国\n");



    //setlocale(LC_ALL,"CN");
    
    //int arrdu[5][4]={10,20,30,40,50,60,70,80,90,100,110,120,130,140,150,160,170,180,190,200};
    int arrdu[5][4]={
        {10,20,30,40},
        {50,60,70,80},
        {90,100,110,120},
        {130,140,150,160},
        {170,180,190,200}       
    };
    //5 行
    int dun=5;
     // 4 列
    int dum=4;
    
     
 
     for(int i = 0; i 

修改一下内存分配,各系统就都可以用。

     //分配内存
//int** pe = (int**)malloc(sizeof(int)*dum);
int** pe =malloc(dum * sizeof(int**));// (int**)malloc(sizeof(int)*dum);
for(int i=0; i

输出:

c: two-dimensional array_第1张图片

c: two-dimensional array_第2张图片

你可能感兴趣的:(C,C++,c语言,开发语言)