The basic operation of a linear function table passed as parameter values passed by reference or analysis

typedef int List;

List *list;

1, the initialization linear form

initList (List * & temp) // must be a reference, because, list has not been initialized and can not be passed by value;

{

temp= (List*)malloc(sizeof(list)*3);

}

2, the destruction of the linear form

destroyList (List * & temp) // should be as reference, because the required list = NULL, if only pointer to free up space, can be passed by value.

{

free(temp);

temp = NULL;

}

3, the n-th element returns

getElem (List * temp, int i , list & value) // value of the operation, then only needs to be a pointer variable value passed to, it can be passed by reference, but is not necessary

{

value = temp[i];

}

4, changing the n-th element

putElem (List * temp, int i , list value) // change operation, then only need a pointer variable values passed to, reference may pass, but not necessary

{

temp[i] = value;

}

For summary 3,4 operation: a value is passed a pointer, which is carried out operating temp = list, they need only be passed by value can operate the memory cell pointed to.

This form:

int a = 1;

int *b = &a;

func(int *temp)
{

* Temp = 2; // or the form temp [0] = 2;

}

func (b); // this case a = 2;

When a pointer is used here for the transmission, or the pointer is * [] treatment solution may be referenced; only if the pointer points to an element, it is understood that only one array element temp [0] to.

Published 25 original articles · won praise 18 · views 10000 +

Guess you like

Origin blog.csdn.net/modi000/article/details/104231548