08-10-2005, 03:28 AM
|
#2 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,693
|
- Function pointers see The C FAQ question 1.34, 4.12 and 20.6 gives a few pointers to where it can be used.
- Considder this typical code:
Code:
#include <stdio.h>
int main(){
int i;
char * str_arr1[] = {"this is first", "this is second", NULL};
char **str_arr2 = {"this is first", "this is second", NULL};
for(i=0; str_arr1[i] != NULL; ++i)
printf("str_arr1: %d : %s\n", i, str_arr1[i]);
for(i=0; str_arr2[i] != NULL; ++i)
printf("str_arr2: %d : %s\n", i, str_arr2[i]);
return 0;
}
char *arg[] is almost the same as char** arg, only issue here is, that in the str_arr1 you explicitly told the compiler it's an array, thus you can assign the placeholders directly. With a char** you need, at runtime, to check for allocation of the needed memory in order to fill your array, since the compiler dosn't know what you're planing to do with it, it will issue a warning in this case, and your program will most likely give a segmentation fault.
- extern only means you can access them througout your entire running code, which means you can define the exern variables in another file, the assign them a valu in another file and access them throughout every function and reassign them in another or read the value in a third function, take a look at this code
Code:
//baz.h
extern int baz;
Code:
//test.c
#include <stdio.h>
#include "baz.h"
int baz = 0;
void foo(void);
void bar(void);
int main(){
int i;
for(i=-1; i < baz; ++i){
if(baz > 3)
bar();
else
foo();
}
}
void foo(void){
printf("Calling foo(), baz = %d\n", baz);
baz++;
}
void bar(void){
printf("Calling bar(), baz = %d\n", baz);
baz--;
}
|
|
|