當前位置:首頁 » 編程語言 » c語言指針計算比分
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言指針計算比分

發布時間: 2022-10-17 19:06:31

c語言 數組指針計算

在c語言中,當把數組名賦值給指針時,那麼該指針指向數組的第一個元素,也就是該指針變數存放的是數組第一個元素的地址。所以
執行語句 int
a[5]={1,3,5,7,9},*p=a;之後,
p指向a[0],
而上面for循環中p<a+5等價於p<=a+4表示只要指針指向的元素地址小於等於數組a中的a[4]的地址,就輸出p指向的元素。
而p++表示使指針p指向a的下一個元素,
所以結果自然是依次輸出a[0],a[1],a[2],a[3],a[4]的值,即13579了

❷ C語言指針運算詳解

C語言中的數組是指 一類 類型,數組具體區分為 int 類型數組,double類型數組,char數組 等等。同樣指針 這個概念也泛指 一類 數據類型,int指針類型,double指針類型,char指針類型等等。
通常,我們用int類型保存一些整型的數據,如 int num = 97 , 我們也會用char來存儲字元: char ch = 'a'。
我們也必須知道:任何程序數據載入內存後,在內存都有他們的地址,這就是指針。而為了保存一個數據在內存中的地址,我們就需要指針變數。
因此:指針是程序數據在內存中的地址,而指針變數是用來保存這些地址的變數。

❸ C語言利用指針編寫函數,輸入3個學生2門課成績,計算每個學生的平均分和每門課的平均分。

給你一個簡單的程序。 希望能看的懂。

#include "stdafx.h"
#include "stdio.h"

int main(int argc, char* argv[])
{
int a,b,c;
printf("a學生成績是:");
int a1,a2;
scanf("%f,%f",&a1, &a2);
printf ("b學生成績是:");
int b1,b2;
scanf ("%d,%d",&b1,&b2);
int c1,c2;
printf("c學生的成績是:");
scanf("%d,%d",&c1,&c2);
a=(a1+a2)/2;
b=(b1+b2)/2;
c=(c1+c2)/2;
printf("a學生的平均成績是:\n");
printf("%d\n",a);
printf("b學生的平均成績是:\n");
printf("%d\n",b);
printf("c學生的平均成績是:\n");
printf("%d\n",c);

int a_c,a_c1;
a_c=(a1+b1+c1)/3;
a_c1=(a2+b2+c2)/3;
printf("每門課的平均成績是:\n");
printf("%d,%d\n",a_c,a_c1);
}

❹ c語言中兩個指針可不可以直接比較值

可以比較,比如
int a[2] = {2,1};
int *P1 = &a[1];
int *P2 = &a[2];

如果指針指向的存儲值比較,就是,*P1>*P2;
如果是指針地址比較,就是,P1<P2;

❺ c語言指針練習題動態內存分配方式計算學生成績

參考
void getMemeory1(char *p)
{
p = (char *)malloc(100*sizeof(char));
}

char * getMemeory2()
{
char p[100] = "Hello world";
return p;
}

int main()
{
//test1
char *str1 = NULL;
getMemeory1(str1);
strcpy(str1, "Hello world");
printf("str1 = %s\n", str1);

//test2
char *str2 = NULL;
str2 = getMemeory2();
printf("str2 = %s\n", str2);
}

執行結果:test1程序報錯,test2列印亂碼或者「Hello world」(一般亂碼)

結果分析:
test1:
在函數中給指針分配空間,實際上是給指針的臨時變數分配空間,函數結束後,這個臨時變數也消亡,而str仍然為NULL,沒有為其分配空間,此時strcpy()是肯定會出錯的。

test2:
1、可能是亂碼,也有可能是正常輸出,因為GetMemory返回的是指向「棧內存」的指針,該指針的地址
不是NULL,但其原來的內容已經被清除,新內容不可知,程序員面試寶典里有專門講該部分知識的。
2、因為p的生命周期在GetMemory函數執行完了就被銷毀了,str 指向的是個野指針。

//相關分析資料摘錄
1、指針參數是如何傳遞內存的?

如果函數的參數是一個指針,不要指望用該指針去申請動態內存。以下Test函數的語句GetMemory(str, 200)並沒有使str獲得期望的內存,str依舊是NULL,為什麼?
1. void GetMemory(char *p, int num)
2. {
3. p = (char *)malloc(sizeof(char) * num);
4. }
5. void Test(void)
6. {
7. char *str = NULL;
8. GetMemory(str, 100); // str 仍然為 NULL
9. strcpy(str, "hello"); // 運行錯誤
10. }
復制代碼
示例1.1 試圖用指針參數申請動態內存

毛病出在函數GetMemory中。編譯器總是要為函數的每個參數製作臨時副本,指針參數p的副本是 _p,編譯器使 _p = p。如果函數體內的程序修改了_p的內容,就導致參數p的內容作相應的修改。這就是指針可以用作輸出參數的原因。在本例中,_p申請了新的內存,只是把 _p所指的內存地址改變了,但是p絲毫未變。所以函數GetMemory並不能輸出任何東西。事實上,每執行一次GetMemory就會泄露一塊內存,因為沒有用free釋放內存。

如果非得要用指針參數去申請內存,那麼應該改用「指向指針的指針」,見示例1.2。
1. void GetMemory2(char **p, int num)
2. {
3. *p = (char *)malloc(sizeof(char) * num);
4. }
5. void Test2(void)
6. {
7. char *str = NULL;
8. GetMemory2(&str, 100); // 注意參數是 &str,而不是str
9. strcpy(str, "hello");
10. cout<< str << endl;
11. free(str);
12. }
復制代碼
示例1.2用指向指針的指針申請動態內存

由於「指向指針的指針」這個概念不容易理解,我們可以用函數返回值來傳遞動態內存。這種方法更加簡單,見示例1.3。
1. char *GetMemory3(int num)
2. {
3. char *p = (char *)malloc(sizeof(char) * num);
4. return p;
5. }
6. void Test3(void)
7. {
8. char *str = NULL;
9. str = GetMemory3(100);
10. strcpy(str, "hello");
11. cout<< str << endl;
12. free(str);
13. }
復制代碼
示例1.3 用函數返回值來傳遞動態內存

用函數返回值來傳遞動態內存這種方法雖然好用,但是常常有人把return語句用錯了。這里強調不要用return語句返回指向「棧內存」的指針,因為該內存在函數結束時自動消亡,見示例1.4。
1. char *GetString(void)
2. {
3. char p[] = "hello world";
4. return p; // 編譯器將提出警告
5. }
6. void Test4(void)
7. {
8. char *str = NULL;
9. str = GetString(); // str 的內容是垃圾
10. cout<< str << endl;
11. }
復制代碼
示例1.4 return語句返回指向「棧內存」的指針

用調試器逐步跟蹤Test4,發現執行str = GetString語句後str不再是NULL指針,但是str的內容不是「hello world」而是垃圾。
如果把示例1.4改寫成示例1.5,會怎麼樣?
1. char *GetString2(void)
2. {
3. char *p = "hello world";
4. return p;
5. }
6. void Test5(void)
7. {
8. char *str = NULL;
9. str = GetString2();
10. cout<< str << endl;
11. }
復制代碼
示例1.5 return語句返回常量字元串

函數Test5運行雖然不會出錯,但是函數GetString2的設計概念卻是錯誤的。因為GetString2內的「hello world」是常量字元串,位於靜態存儲區,它在程序生命期內恆定不變。無論什麼時候調用GetString2,它返回的始終是同一個「只讀」的內存塊。

2、杜絕「野指針」

「野指針」不是NULL指針,是指向「垃圾」內存的指針。人們一般不會錯用NULL指針,因為用if語句很容易判斷。但是「野指針」是很危險的,if語句對它不起作用。 「野指針」的成因主要有兩種:

(1)指針變數沒有被初始化。任何指針變數剛被創建時不會自動成為NULL指針,它的預設值是隨機的,它會亂指一氣。所以,指針變數在創建的同時應當被初始化,要麼將指針設置為NULL,要麼讓它指向合法的內存。例如
1. char *p = NULL;
2. char *str = (char *) malloc(100);
復制代碼
(2)指針p被free或者delete之後,沒有置為NULL,讓人誤以為p是個合法的指針。

(3)指針操作超越了變數的作用范圍。這種情況讓人防不勝防,示常式序如下:
1. class A
2. {
3. public:
4. void Func(void){ cout << 「Func of class A」 << endl; }
5. };
6. void Test(void)
7. {
8. A *p;
9. {
10. A a;
11. p = &a; // 注意 a 的生命期
12. }
13. p->Func(); // p是「野指針」
14. }
復制代碼
函數Test在執行語句p->Func()時,對象a已經消失,而p是指向a的,所以p就成了「野指針」。但奇怪的是我運行這個程序時居然沒有出錯,這可能與編譯器有關。

❻ C語言用指針編寫的四則運算

不知道怎麼左對齊,//後面是英文注釋///後面是中文注釋 給採納!!

// EX6_08.CPP
// A program to implement a calculator

#include <stdio.h> // For input/output
#include <stdlib.h> // For the exit() function
#include <ctype.h> // For the isdigit() function
#include <string.h> // For the strcpy() function

void eatspaces(char * str); // Function to eliminate blanks
double expr(char * str); // Function evaluating an expression
double term(char * str, int * pindex); // Function analyzing a term
double number(char * str, int * pindex); // Function to recognize a number
char * extract(char * str, int * index); // Function to extract a substring

const int MAX = 80; // Maximum expression length including '\0'

int main(void)
{
char buffer[MAX]; // Input area for expression to be evaluated
char c;
int j,i;

printf("Welcome to your friendly calculator.\n");
printf("Enter an expression, or an empty line to quit.\n");

for(;;)///無開始無終止只有過程,就是不停的循環,保證可以連續輸入好幾個式子,如先求1+2回車直接3+2再回車,可以多次求
{///式子是一步一步來的,就是說不進行完這個語句是不會進行下一個的
i=0;
scanf("%c",&c); ///捕捉第一個數是c// Read an input line
while(c!='\n')
{
buffer[i++]=c;///,i++是先運算後自加,++i是先自加後運算,所以第一個數放在buffer[0]處
scanf("%c",&c);
}///把式子放在這個數列中,while控制回車時式子結束,此時i是

buffer[i]='\0';///式子最後是一個\o作為標志"\0代表字元數串的結束標志,最後一個在i-1的位置

eatspaces(buffer);///對式子去空格處理 // Remove blanks from input

if(!buffer[0]) // Empty line ends calculator
return 0;///已經去過空格了,buffer[0]應該是有值的,但是沒有的話就直接結束,返還0

printf( "\t= %f\n\n",expr(buffer)); ///結果是expr分函數return的結果 // Output value of expression
}
}

// Function to eliminate blanks from a string
void eatspaces(char * str)///預處理,把指向數組的指針設為*str,其中數組中數的地址都是相連的,char數組每個地址差1,int數組每個差2依次類推,其中char型的指針加1就是加一,int型的加一是加二
{
int i=0; // 'Copy to' index to string
int j=0; // 'Copy from' index to string

while((*(str+i) = *(str+j++)) != '\0')///這里是對*(str+i)的一個賦值,沒有空格時每次i都加1式子有空格時(str+i)和(str+j++)是相等的,
/// 當有空格時i不加一,就讓空格這個地址等於下一個地址,即把式子向前推了一個字元,直到\0 // Loop while character copied is not \0
if(*(str+i) != ' ') // Increment i as long as
i++; // character is not a blank
return;
}

// Function to evaluate an arithmetic expression
double expr(char * str)///真正處理,對加減的分函數
{
double value = 0; ///value是結果,也就是真實值 // Store result here
int index = 0; // Keeps track of current character position

value = term(str, &index);///先解決第一步,因為數字還是char型根本沒法算,先把第一個數變成數順便有()*/都做完了直到看見-+ // Get first term

for(;;) ///保證了多加的情況,後同 // Infinite loop, all exits inside
{
switch(*(str+index++)) ///通過加數組對應的地址時數組一個一個往後推,碰到\0 + -做判斷 其他符號輸出錯誤 // Choose action based on current character
{
case '\0': ///一直到最後的標記,輸出 // We're at the end of the string
return value; // so return what we have got

case '+': ///先把第一個數變成數然後有()就先()有/*就先/* 最後不都沒有了就到-+瞭然後-+後面的也要先乘除啊,就value += term(str, &index);這樣也能讓後面的也能變成數,也能讓後面的先*/依次類推 // + found so add in the
value += term(str, &index); // next term
break;

case '-': // - found so subtract
value -= term(str, &index); // the next term
break;

default: // If we reach here the string
printf("Arrrgh!*#!! There's an error.\n");///其他符號輸出錯誤因為先進行的*、和()運算,所以再不是-+就一定錯了
exit(1);
}
}
}

// Function to get the value of a term
double term(char * str, int * pindex)///對乘除的分函數
{
double value = 0; // Somewhere to accumulate the result

value = number(str, pindex); // Get the first number in the term

// Loop as long as we have a good operator
while((*(str+(*pindex))=='*')||(*(str+(*pindex))=='/'))///進來的是/或者*才繼續,不然直接輸出了
{

if(*(str+(*pindex))=='*') // If it's multiply,
{
++(*pindex);///是乘先加,就是乘的是後面的
value *= number(str, pindex); ///通過這樣,先括弧後乘除解決第一步以後的 // multiply by next number
}

if(*(str+(*pindex))=='/') // If it's divide,
{
++(*pindex);
value /= number(str, pindex); // divide by next number
}
}
return value; // We've finished, so return what we've got
}

// Function to recognize a number in a string
double number(char * str, int * pindex)///對(的分函數
{
double value = 0.0; // Store the resulting value

char * psubstr; // Pointer for substring
if(*(str + (*pindex)) == '(') // Start of parentheses
{
++(*pindex);
psubstr = extract(str, pindex);///先看後括弧 // Extract substring in brackets
value = expr(psubstr); ///打回來以後再直接用大的順序進行計算 // Get the value of the substring
return value; // Return substring value
}
///以下是將我們在數組中定義為char的數變為int形式
while(isdigit(*(str+(*pindex)))) ///isdidit是調用的<ctype.h> 這個庫函數的一種函數,判斷字元是否為阿拉伯數字 // Loop accumulating leading digits
value=10*value + (*(str+(*pindex)++) - 48);///因為ASC碼48就是'0',也就是說'0'的值是48,而後依次是'1'到'9'。這樣正好是char型的減去48就是它對應的int值
///這樣層層推進,直到value是這個數為止(如125就會定義為三個char變數,先判斷1是阿拉伯數字,讓它乘10加2就成12,再看5是阿拉伯數字,12乘10加5就成了125,變成阿拉伯數字了)

// Not a digit when we get to here

if(*(str+(*pindex))!='.') ///如果沒有小數點可以回去了,這里小數點顯然無法通過上面的isdigit,就是會有123.45這原來是六個字元(1/2/3/。/4/5)通過上面變成了(123/。/4/5)其中4/5還是char型 // so check for decimal point
return value; // and if not, return value

double factor = 1.0; // Factor for decimal places
while(isdigit(*(str+(++(*pindex)))))///當檢測到小數點後面是數的時候就會進行 // Loop as long as we have digits
{
factor *= 0.1; // Decrease factor by factor of 10
value=value + (*(str+(*pindex))-48)*factor;///這里直接第一輪123加0.1*4,第二輪再加0.01*5 // Add decimal place
}

return value; // On loop exit we are done
if(*(str+(*pindex))!='.') // so check for decimal point
return value; ///在此看後面有無小數點,沒有就回去,有的話就雙小數點,沒有返回值發生錯誤 // and if not, return value

}

// Function to extract a substring between parentheses
// (requires string.h)
char * extract(char * str, int * pindex)///對)的分函數
{
char buffer[MAX]; // Temporary space for substring
char * pstr = NULL; ///代表一個空指針 // Pointer to new string for return
int numL = 0; // Count of left parentheses found
int bufindex = *pindex;///這個定義是 bufindex直接指向當前所指了 // Save starting value for index
do///多次循環
{
buffer[(*pindex) - bufindex] = *(str + (*pindex));///這相當於重新引進了一套目的是不會混亂
switch(buffer[(*pindex) - bufindex])
{
case ')':///當遇到)時
if(numL == 0)///第一次就代表只有()沒有套((()))這種情況,((()))會一層一層的算
{
buffer[(*pindex) - bufindex] = '\0'; ///就直接算()裡面的把)當成結束去算 // Replace ')' with '\0'
++(*pindex);
pstr = (char *) malloc((*pindex) - bufindex + 1);///malloc是申請使用((*pindex) - bufindex + 1)大小的空間然後指向這個空間的指針設為pstr
if (!pstr)///指針無效就會運行下面這個
{
printf("Memory allocation failed, program terminated.") ;
exit(1);
}
strcpy(pstr, buffer); /// strcpy也是一個函數復制字元串買就是吧buffer的復制到 pstr中去// Copy substring to new memory
return pstr; ///把()裡面的東西直接打回去。 // Return substring in new memory
}
else
numL--; // Rece count of '(' to be matched
break;
case '(':
numL++; ///說明有((()))這樣的結構,用numl記錄有幾個(,有一個)減一個1 // Increase count of '(' to be // matched
break;
}
} while(*(str + (*pindex)++) != '\0'); ///沒有)直接結束會輸出這句話 // Loop - don't overrun end of string
printf("Ran off the end of the expression, must be bad input.\n");
exit(1);
}

❼ 用C語言結構體指針編程序實現輸入十個學生的學號,期中和期末成績,計算輸出成績表和學生平均分

#include<iostream>
#include<string>

using namespace std;
//=============<開始定義結構體>===================================================
struct combox{

int num;
int mark;
string name;
combox *next;

};
//=============<結束定義結構體>===================================================

//=============<開始定義Commonbox類>==============================================

//-----類體開始------------------------
class Commonbox{

private:

combox *head;void Swap(combox *,combox *); //交換兩個combox變數的數據域

void Print(combox *); //輸出一combox指定的記錄

combox *Find(int); //查找條例條件的記錄,並返回該記錄的指針

public:

Commonbox(){head=NULL;}

int ListCount(); //統計當前鏈表的記錄總數,返回一個整數

void AddItem(int num, string name, int mark); //添加一條記錄到表尾
void RemoveItem(int); //刪除一條指定的記錄

void List(); //列出當前鏈表中的所有記錄

void Sort(); //對當前鏈表進行排序

void Search(int); //在當前鏈表查找指定記錄並輸出

float Average(); //計算平均成績

(7)c語言指針計算比分擴展閱讀

用C語言結構體指針編程序應用方法:

將一個結構體變數中的數據傳遞給另一個函數,有下列3種方法:

(1) 用結構體變數名作參數。一般較少用這種方法。

(2) 用指向結構體變數的指針作實參,將結構體變數的地址傳給形參。

(3) 用結構體變數的引用變數作函數參數。

在軟體開發過程中,常常需要動態地分配和撤銷內存空間,例如對動態鏈表中結點的插入與刪除。C語言中是利用庫函數malloc和free來分配和撤銷內存空間的。

❽ C語言指針的計算搞不懂。

你可以把指針看出是一個一維數組,其實指針就是數組,數組就是拿指針來實現的!
1、程序中把a[5]的地址給了指針p,即p指針指向了a[5]
2、那麼把p看成數組,因為是int,所以可以存在負的裡面,則p看成數組為p[0]=a[5],
3、那麼p[-2]則是a[3]對應的值為54
4、你可以驗證這樣寫看if(&p[-2]!=&a[3]),如果相等則驗證了,事實就是相等
希望可以幫到你,如果滿意請採納!

❾ c語言 數組指針計算

#include<stdio.h>
main(){
//設數組a的首地址為2000,
int a[3][4]={{1,3,5,7},{9,11,13,15},{17,19,21,23}};
printf("a[2][1]=%d\n",a[2][1]);// 得值19
printf("a[1]=%d\n",a[1]);//9的地址2016
printf("a =%d\n",a);//1的地址2000
printf("a+1 =%d\n",a+1);//9的地址2016
printf("*a+1=%d\n",*a+1);//3的地址2004
printf("*(a+1)=%d\n",*(a+1));//9的地址2016
printf("a[2]+1=%d\n",a[2]+1);//19的地址2036
printf("*(a+1)+1=%d\n",*(a+1)+1);//11的地址2020
printf("*(*(a+2)+2)=%d\n",*(*(a+2)+2));//得值21
}

//以上結果我試過了