Pointers on C, Notes

Pointers on C, Notes

rearrange( char *output, char const *input, in n_columns, int const columns[] ) //define as pointer, use as name input

int *a; a pointer named ‘a’ point to a int.

char *message=”Hello world!”; *“Hello world!” is assigned to message not *message * //equals to char *message; message=”Hello world!”

int *pi; // pointer to integer int const *pci; //pointer to a const int. int * const cpi; // const pointer to a int. int const * const cpci; // both pointer and int are constants

block scope{}/ File Scope/prototype scope/Function scope

?? do not unstand

variable declared outside block–> static memory

within block –> (automatic)on the stack

within block+static–> static, exist with the entire duration of the program

with key word register –> hardware register

### array as input var=pointer ###

when array as input, the input variables are pointers indeed. The sizeof() return 1 for char, return 4 for pointer, return the size of a char array. for the codes blow,

main(void){
	char src[]="abcdefghijklmnopqrsguvwxyz1234567890+-~";
	char dst[30];
 	int i=0;

 	int rst=substr(dst,src,4,40);
 	printf("rst=%d  dst=%s\n",rst,dst);
}

int substr(char dst[], char src[], int start, int len){
	int i=-1;

	int srcLen=strlen(src);
	int dstLen=strlen(dst);
	if(start<0 || start>=srcLen || len<0) return i;	
	if(len+start<srcLen){
		for(i=start;i<len+start;i++) dst[i-start]=src[i];		
		dst[len]='\0';

	}else{
		for(i=start;i<srcLen;i++) dst[i-start]=src[i];
		dst[srcLen]='\0';
	}
	return i-start;
}

the src[] and dst[] in substr are pointers. so, when you call sizeof(src) inside the substr, the return value is 4(the size of a pointer).

Jiayang, Sun 12 February 2016
blog comments powered by Disqus