首页 > 其他分享 >指针和字符串初认识

指针和字符串初认识

时间:2023-02-11 20:00:31浏览次数:49  
标签:rows const 认识 void cols element int 字符串 指针

指针练习和字符串初认识

一:对二维数组内的元素乘以2

#include<stdio.h>
#define COLS 5
#define ROWS 3 
void show_element(int rows, int cols, const int t[rows][cols]);//打印函数不修改数组元素,需使用const 
void double_element(int rows, int cols,  int t[rows][cols]);
int main(void)
{
    int arr[ROWS][COLS]={{1,0,4,6,9},{2,5,6,8,3},{5,3,21,1,7}}; 
    show_element(ROWS,COLS,arr);
    double_element(ROWS,COLS,arr);
    printf("\n");
    show_element(ROWS,COLS,arr);
    return 0;
}
​
void show_element(int rows, int cols, const int t[rows][cols])
{
    int i,j;
    for(i=0;i<rows;i++){
        for(j=0;j<cols;j++){
            printf("%4d",t[i][j]);
        }
    }
    
}
​
void double_element(int rows, int cols,  int t[rows][cols])
{
    int i,j;
    for(i=0;i<rows;i++){
        for(j=0;j<cols;j++){
            t[i][j]*=2;
        }
    }
}

运行结果:

  1   0   4   6   9   2   5   6   8   3   5   3  21   1   7
   2   0   8  12  18   4  10  12  16   6  10   6  42   2  14
  1. 程序内的打印函数元素加倍函数均使用变长数组作为形参,使用传统的方式定义两个函数的语句如下:

void show_element(int rows, const int t[][cols]);
void double_element(int rows,  int t[][cols]);
  1. 打印函数不修改数组元素,需使用const关键字修饰,加倍函数则不能使用const修饰二维数组的参数

  2. 在刚开始时,在define处发生错误提示,是因为定义了两个相同的变量,一个宏变量ROWS 和COLS,还有void show_element( ); void double_element( ); 里的ROWS和COLS当两个相同宏变量出现的时候就会发生[Error] expected ';', ',' or ')' before numeric constant编译报错的问题.只需要把函数里的int N改成其他变量名或修改宏变量名,就可以编译通过

  3. 解释连接 发生[Note] expected 'const int (*)[(sizetype)(cols)]' but argument is of type的错误的原因

字符串初认识

  1. 字符串是以空字符(\0)结尾的char类型数组

#include<stdio.h>
int main(void)
{
    u7char *str="Hello";//指针指向了一个字符数组
    char word[]="Hello";
    char line[10]="Hello";//数组的大小为10字节,放入的字符串有5个字符,在line中占6个字节(最后还有一个0) 
    
}
 

标签:rows,const,认识,void,cols,element,int,字符串,指针
From: https://www.cnblogs.com/ninnne/p/17112459.html

相关文章