c


C语言关键---static

百般思念 提交于 2020-02-28 13:40:48
static 这个关键字,我感觉对很多初级程序源和刚接触程序的同学来说,可能都没有完全理解它的含义。至少对我来说对它的认识不是很清楚。因此对它在次总结一下。主要作为自己的备忘录。 1.static关键字的第一特性: 隐藏 static 在修饰变量时分为全局变量和局部变量,都存在静态存储区域。 static全局变量:作用域是从定义的位置开始到整个所在cpp文件结束,其他cpp文件都不能使用static定义的变量,在同一个cpp文件中,在定义前使用要用extern关键字。因此static修饰变量会被隐藏在一个cpp文件里。 static 局部变量:作用域只在一个函数的内部使用,static局部变量是静态存储,因此在函数退出后,仍然保存值,再次进入函数时,static局部变量使用上次的值。 在局部变量前,加上关键字static,该变量就被定义成为一个静态局部变量。 我们先举一个静态局部变量的例子,如下: //Example 3 #include <iostream.h> void fn(); void main() { fn(); fn(); fn(); } void fn() { static n=10; cout<<n<<endl; n++; }   通常,在函数体内定义了一个变量,每当程序运行到该语句时都会给该局部变量分配栈内存。但随着程序退出函数体,系统就会收回栈内存

C Primer Plus 第9章 函数 9.7 指针简介

坚强是说给别人听的谎言 提交于 2020-02-28 11:46:51
一般来讲, 指针是一个其数值为地址的变量 (或更一般地说是 一个数据对象 )。 正如char类型的变量用字符作为其数值,而int类型变量的数值是整数,指针变量的数值表示的是地址。 如果您将某个指针变量命名为ptr,就可以使用如下语句: ptr=&pooh; /*把pooh的地址赋给ptr*/ 对于这个语句, 我们称ptr指向pooh 。 ptr和&pooh的区别在于前者为一变量,而后者是一个常量。 当然,ptr可以指向任何地方: ptr=%bah; /*令ptr指向bah而不是pooh*/ 这时,ptr的值是bah的地址。 要创建一个指针变量,首先需要声明其类型。假设您想把ptr声明为可以存放一个int数值的地址,就需要 使用下面介绍的新运算符。 9.7.1 间接运算符 假定ptr指向bah,如下所示: ptr = &bah; 这时就可以 使用间接(indirection)运算符*(也称作取值(dereferencing))来获取bah中存放的数值 (不要把这种一元运算符和表示乘法的二元运算符*相混淆)。 val = *ptr; /*得到ptr指向的值*/ 语句ptr=&bah;以及语句val=*ptr;放在一起等同于下面的语句: val = bah; 由此看出,使用地址运算符和间接运算符可以间接完成上述语句的功能,这也正是“间接运算符”名称的由来。 9.7.2 指针声明

Why can't local variable be used in GNU C basic inline asm statements?

柔情痞子 提交于 2020-02-28 09:46:20
问题 Why cannot I use local variables from main to be used in basic asm inline? It is only allowed in extended asm, but why so? (I know local variables are on the stack after return address (and therefore cannot be used once the function return), but that should not be the reason to not use them) And example of basic asm: int a = 10; //global a int b = 20; //global b int result; int main() { asm ( "pusha\n\t" "movl a, %eax\n\t" "movl b, %ebx\n\t" "imull %ebx, %eax\n\t" "movl %eax, result\n\t"

sending Iplimage using sockets

試著忘記壹切 提交于 2020-02-28 08:21:49
问题 I am trying to capture frames from a Logitech HD cam connected to a Raspberry Pi through usb, the RP is running arch linux and I am using OpenCV C api and a TCP client. The TCP server is running c++(QT) under ubuntu. I am storing the frame->imageData in a buffer and then send the data in the buffer. this is a proof of concept that I can save a frame to a buffer then reconstruct a new frame which is tmpbuffer from the buffer,actually this code is running perfectly: CvCapture *capture =

How do you pass a typedef struct to a function?

坚强是说给别人听的谎言 提交于 2020-02-28 08:12:32
问题 At the moment I'm trying void avg(everything) But that gives me the error: error: subscripted value is neither array nor pointer And when I got this error earlier today it was because I wasn't passing a 2D array to the function properly. So I figure this is the same but I can't find the correct format to pass it in. This is my typedef: typedef struct structure { char names[13][9]; int scores[13][4]; float average[13]; char letter[13]; } stuff; And this is my typedef array: stuff everything[13

How do you pass a typedef struct to a function?

大兔子大兔子 提交于 2020-02-28 08:08:28
问题 At the moment I'm trying void avg(everything) But that gives me the error: error: subscripted value is neither array nor pointer And when I got this error earlier today it was because I wasn't passing a 2D array to the function properly. So I figure this is the same but I can't find the correct format to pass it in. This is my typedef: typedef struct structure { char names[13][9]; int scores[13][4]; float average[13]; char letter[13]; } stuff; And this is my typedef array: stuff everything[13

sizeof(array) in C: segmentation fault [duplicate]

£可爱£侵袭症+ 提交于 2020-02-28 07:27:24
问题 This question already has answers here : Getting a stack overflow exception when declaring a large array (8 answers) Closed 3 years ago . Hi i get a weird segmentation fault from this code: int main(void){ int array1[10000000]; int n = sizeof(array1); printf("%d \n", n ); return 0; } However if i change int array1[10000000]; to int array1[1000000]; ( one less zero) The program works and prints 4000000 I'm running it on Fedora 21(64bits) Is this because there is a maximum size for array in C?

Android JNI native C function call kills activity

微笑、不失礼 提交于 2020-02-28 06:04:14
问题 What works: I have a c executable that runs a TUN/TAP service, and two shell scrips (to configure the "ip route" and "iptables") that run fine in terminal, all run as root. What doesn't work: I am attempting to create an Android App to run the c executable and shell scripts after a button is pressed. I originally made it such that onClick would create a process with processBuilder as shown below: final Button button1 = ... ... public void onClick(View v) { String ip_address = edIPAddress

What events can cause ferror to return non-zero?

你。 提交于 2020-02-28 04:53:47
问题 What events can cause ferror() to return non-zero and after what events should one check ferror() ? http://www.cplusplus.com/reference/cstdio/ferror/ Opening, reading, closing? Will the return value of ferror() ever change spontaneously? E.g., if a program checks ferror(stream) , takes a nap without interacting with the FILE object associated with stream , and then checks ferror(stream) again, will the return value ever be different? Is any of this mandated by standards? 回答1: It is primarily

Can still print a string after I freed it?

半世苍凉 提交于 2020-02-27 23:20:39
问题 I am learning and testing memory allocation in C and I want to test what happens if free() is called. I expected there could be a segmentation fault or pointer is NULL after I run the program below. However, I can still successfully print the string as in Output. I also tried to free str twice, then an error as Output 2 occurred. It seems the previously allocated memory is successfully deallocated, but the data on the memory is not cleaned up. Is that correct? If that is the case, when will

工具导航Map