About C pointer...

本文回顾了作者在大学期间学习C语言指针的经历,并详细介绍了指针的基础概念及其应用场景,包括指向变量、作为参数改变全局变量的值、以及函数指针等。文章通过实例解释了如何使用指针,并对比了C语言与C++中引用的区别。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

刚才对C语言指针进行了全面的学习,看了不少资料,学到了不少东西。

记得大一下学期就学了C语言了,当初老师只教了些基本的东西,后来讲指针只是带过了,以没时间为由结束了C的学习,这个后来的C数据结构的学习带来了问题。没有学好C指针,数据机构的例子也就很难看懂,这更加大了学习数据结构的难度。还是由于老师的关系(数据结构的老师也就是C语言老师),很大原因是老师的普通话太差劲了,听起课来特别费力,这大大打击了我学习数据结构这门课的热情。

今天,我准备参加ACM比赛,虽然我的数据结构不怎么样,C语言也很一般,但是我还是参加了,这是一次难得的机会,对于快要结束四年大学生活的我来说,是时候在大学的时候留下点什么了。

过去的找不回来了,只能用现在的努力来弥补。说了这么多,还是回到正题吧。

众所周知,指针是用来指向内存地址的变量,而所有的变量在内存中都分配有一定的空间,每个内存空间都有自己的地址。正由于这个特性,所以指针可以做很多事。如:

1.指向某个变量的。

2.作为参数,可以改变全局变量的值,也叫变参。

3.函数指针,感觉有点像C++ 的模板函数,但又不是。

……

具体举例:

1. Example:

int * p;

int a=9;

p=&a;

printf("Result: %d /n",*p);

Ouput:Result: 9

2.Example:

In C:

// Takes the value of interest by reference and adds 2.
void C(int* worthRef) {
*worthRef = *worthRef + 2;
}
// Adds 1 to the value of interest, and calls C().
void B(int* worthRef) {
*worthRef = *worthRef + 1; // add 1 to value of interest as before
C(worthRef); // NOTE no & required. We already have
// a pointer to the value of interest, so
// it can be passed through directly.
}

But in C++ is:

void Swap(int& a, int& b) { // The & declares pass by reference
int temp;
temp = a; // No *'s required -- the compiler takes care of it
a = b;
b = temp;
}
void SwapCaller() {
int x = 1;
int y = 2;
Swap(x, y); // No &'s required -- the compiler takes care of it
}

注意变参 int* a和int& a,效果是一样的,但在C中int& a是编译不通过的。

3.Example:

一个很好的例子:

//自行包含头文件

void CallMyFun(FunType fp,int x) //③. 参数fp的类型是FunType。

{

fp(x);//④. 通过fp的指针执行传递进来的函数,注意fp所指的函数是有一个参数的

}

void MyFun1(int x) // ①. 这是个有一个参数的函数,以下两个函数也相同

{

printf(“函数MyFun1中输出:%d/n”,x);

}

void MyFun2(int x)

{

printf(“函数MyFun2中输出:%d/n”,x);

}

void MyFun3(int x)

{

printf(“函数MyFun3中输出:%d/n”,x);

}

 

typedef void (*FunType)(int ); //②. 定义一个函数指针类型FunType,与①函数类型一至

void CallMyFun(FunType fp,int x);

int main(int argc, char* argv[])

{

CallMyFun(MyFun1,10); //⑤. 通过CallMyFun函数分别调用三个不同的函数

CallMyFun(MyFun2,20);

CallMyFun(MyFun3,30);

}

//-----------------------------------------------------------------

自己理解:

声明函数类型指针:int (*FunName)(int[,int]...);

数据结构中遍历的例子:

Status PreOrderTravel(LinkList *L,ElementType e,Status (*View)(ElementType))

{

表达式……

……

View(e);

……

}

这里的Status(*View)(ElementType)就是一个函数指针。

使用这个函数之前必须对他进行定义:Status printTravel(ElementType e){printf("  %d  /n",e);return OK;}

直到现在我们才可以调用函数PreOrderTravel(LinkList *L,ElementType e,Status (*View)(ElementType));

应该这样调用他:PreOrderTravel(&L,e,printTravel);//printTravel这里将函数地址传给PreOrderTravel函数中的View函数。

That's all,thank you ! Power By LongLongAgo

 
Executing: nios2-swexample-create --describeAll (c:\altera\13.1\quartus\bin64) 1 [main] bash 55472 find_fast_cwd: WARNING: Couldn't compute FAST_CWD pointer. Please report this problem to the public mailing list cygwin@cygwin.com 1 [main] uname 31748 find_fast_cwd: WARNING: Couldn't compute FAST_CWD pointer. Please report this problem to the public mailing list cygwin@cygwin.com <nios2-swexample name="float2_gcc" display_name="Float2 GCC"> Float2 GCC Example shows how to infer the floating point operations provided by the Floating Point Hardware 2 component. A user may inspect the objdump file to see the code generated by GCC. The Floating Point Hardware 2 component is the 2nd generation of floating-point custom instructions for Nios II. It offers improved performance, hardware acceleration of more operations, and reduced resource usage relative to the 1st generation. Results are not fully IEEE 754 compliant due to the implementation of simplified rounding. System Requirements ******************* The following component must be connected to the Nios II: - 'Floating Point Hardware 2' component </nios2-swexample> <nios2-swexample name="hello_world_small" display_name="Hello World Small"> Hello World Small prints 'Hello from Nios II' to STDOUT. The project occupies the smallest memory footprint possible for a hello world application. This example runs with or without the MicroC/OS-II RTOS and requires an STDOUT device in your system's hardware. For details, click Finish to create the project and refer to the readme.txt file in the project directory. The BSP for this template is based on the Altera HAL operating system with reduced code footprint. For information about how this software example relates to Nios II hardware design examples, refer to the Design Examples page of the Nios II documentation available with your
最新发布
03-22
### Differences Between `string.h` and `std::string` In C programming, handling strings can be done using two primary methods: through functions provided by the standard library header `<string.h>` or via the C++ Standard Library class `std::string`. #### Functions Provided by `<string.h>` The `<string.h>` header offers a set of low-level operations for manipulating arrays of characters (null-terminated byte strings). These include: - **Copying Strings**: The function `strcpy(dest, src)` copies the string pointed to by `src`, including the terminating null character, into the array pointed to by `dest`. - **Concatenating Strings**: Using `strcat(s1, s2)`, appends a copy of the suffix of `s2` to the end of the string `s1`. Both must be properly null terminated. - **Comparing Strings**: With `strcmp(s1, s2)`, compares lexicographically the null-terminated string `s1` with the string `s2`. These functions operate directly on raw pointers representing character sequences. They require manual management of memory allocation and deallocation when dynamically allocating space for strings[^1]. ```c #include <stdio.h> #include <string.h> int main() { char dest[50]; strcpy(dest, "Hello"); strcat(dest, " World!"); printf("%s\n", dest); } ``` #### Class `std::string` Introduced in C++, `std::string` provides higher level abstractions over traditional C-style strings. This container manages its own storage automatically, allowing more intuitive manipulation without worrying about buffer sizes or overflow issues. Key features include: - **Automatic Memory Management**: Automatically resizes internal buffers as needed. - **Operator Overloading Support**: Enables natural syntax like concatenation (`+`) between objects. - **Rich Set of Member Functions**: Includes search capabilities, insertion/deletion at arbitrary positions within the sequence among others. Using `std::string` leads generally safer code since many common errors associated with pointer arithmetic are avoided[^3]. ```cpp #include <iostream> #include <string> using namespace std; int main(){ string greeting = "Hello"; greeting += " world!"; cout << greeting; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值