日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

詳解C語(yǔ)言結(jié)構(gòu)體中的char數(shù)組如何賦值_C 語(yǔ)言

作者:北極的大企鵝 ? 更新時(shí)間: 2022-05-05 編程語(yǔ)言

前景提示

定義一個(gè)結(jié)構(gòu)體,結(jié)構(gòu)體中有兩個(gè)變量,其中一個(gè)是char類型的數(shù)組,那么,怎么向這個(gè)數(shù)組中插入數(shù)據(jù),打印數(shù)據(jù)呢?

 typedef struct SequenceList {
	// 數(shù)組的元素
	char element[20];
	// 數(shù)組的長(zhǎng)度
	int length;
};

定義一個(gè)結(jié)構(gòu)體,結(jié)構(gòu)體中有兩個(gè)變量,其中一個(gè)是char類型的數(shù)組指針,那么,怎么向這個(gè)數(shù)組中插入數(shù)據(jù),打印數(shù)據(jù)呢?

 // 定義順序表結(jié)構(gòu)體
typedef struct SequenceList {
	char *elment;
	int length;
};

這里的結(jié)構(gòu)體處理的步驟

  • 結(jié)構(gòu)體初始化
  • 結(jié)構(gòu)體內(nèi)數(shù)據(jù)賦值
  • 結(jié)構(gòu)體內(nèi)輸出數(shù)據(jù)

本著上述的原則,先對(duì)第一種類型進(jìn)行操作

一.char數(shù)組類型的處理

1.結(jié)構(gòu)體初始化

         SequenceList L;
	L.element = (char*)malloc(sizeof(char)*10);
	L.length  = 10

2.結(jié)構(gòu)體內(nèi)數(shù)據(jù)賦值(簡(jiǎn)單法)

    L.elment[0] = 1;
    L.elment[1] = 2;
    L.elment[2] = 3;
    L.elment[3] = 4;
    L.elment[4] = 5;

for循環(huán)

      for (int i = 0; i < 10; i++)
    {
        L.elment[i] = i+1;
    }

3.結(jié)構(gòu)體內(nèi)輸出數(shù)據(jù)

  for (int i = 0; i < 10; i++)
    {
        //不會(huì)打印空值
        if (L.elment[i]>0) {
            printf("element[%d] = %d\n",i, L.elment[i]);
        }
    }

二.char數(shù)組指針類型的處理

1.結(jié)構(gòu)體初始化

   //結(jié)構(gòu)體初始化
   MyList L;
   L.length = LENGTH;
   L.elment = (char*)malloc(L.length * sizeof(char));

2.結(jié)構(gòu)體內(nèi)數(shù)據(jù)賦值

    //結(jié)構(gòu)體賦值
    for (int i = 0; i < LENGTH; i++)
    {
        *(L.elment + i) = 'A' + i;
    }

3.結(jié)構(gòu)體內(nèi)輸出數(shù)據(jù)

   //打印結(jié)構(gòu)體中的值
    for (int i = 0; i < LENGTH; i++)
    {
        if (*(L.elment + i) > 0) {
            printf("elment[%d] = %c\n", i, *(L.elment + i));
        }
    }

三.全部代碼

1. char數(shù)組

// 010.順序表_004.cpp : 此文件包含 "main" 函數(shù)。程序執(zhí)行將在此處開(kāi)始并結(jié)束。
//
#include 
#define MAXSIZE 10
 
typedef struct SequenceList {
	// 數(shù)組的元素
	char element[MAXSIZE];
	// 數(shù)組的長(zhǎng)度
	int length;
};

int main()
{
	// 1.初始化結(jié)構(gòu)體
	SequenceList *L;
	L = (SequenceList*)malloc(sizeof(char)*MAXSIZE);
	L->length = MAXSIZE;
 
	// 2.存入結(jié)構(gòu)體內(nèi)值
	for (int i = 0; i < MAXSIZE; i++)
	{
		L->element[i] = 'a' + i;
	}
 
	// 3.打印結(jié)構(gòu)體內(nèi)的值
	for (int i = 0; i < MAXSIZE; i++)
	{
		if (*(L->element + i) > 0) {
			printf("elment[%d] = %c\n", i, *(L->element + i));
		}
	}
}

2. char數(shù)組指針

// 011.順序表_005.cpp : 此文件包含 "main" 函數(shù)。程序執(zhí)行將在此處開(kāi)始并結(jié)束。
//
#include 
#define MAXSIZE 10

typedef struct SequenceList {
	// 數(shù)組的元素
	char *element;
	// 數(shù)組的長(zhǎng)度
	int length;
};
 
int main()
{
	// 1.結(jié)構(gòu)體初始化
	SequenceList L;
	L.length = MAXSIZE;
	L.element = (char*)malloc(L.length * sizeof(MAXSIZE));
 
	// 2.結(jié)構(gòu)體內(nèi)賦值
	for (int i = 0; i < MAXSIZE; i++)
	{
		*(L.element + i) = 'a' + i;
	}
	
	// 3.打印結(jié)構(gòu)體中的值
	for (int i = 0; i < MAXSIZE; i++)
	{
		if (*(L.element + i) > 0) {
			printf("elment[%d] = %c\n", i, *(L.element + i));
		}
 
	}
}

結(jié)語(yǔ)這就是最近遇到的問(wèn)題,這個(gè)問(wèn)題困擾了很久,相信許多的初學(xué)者也遇到了這樣的問(wèn)題,但是,網(wǎng)上的描述根本不怎么好用,所以,希望本博主遇到的這個(gè)問(wèn)題能幫助到你

總結(jié)

原文鏈接:https://www.cnblogs.com/liuyangfirst/p/15964945.html

欄目分類
最近更新