포인터를 이용한 함수의 호출



----------------------------code--------------------------------------------------

#include <iostream> using namespace std; int compare(void* item1, void* item2){ if( *(int*)item1>= *(int*)item2){ return 1; }else return 0; } void* larger(void* item1, void* item2, int (*compare)(void* , void*) ){ if( (*compare)(item1, item2) >0 ){ return item1; }else{ return item2; } } void main(){ int a= 6; int b=7; cout<< *(int*)larger(&a, &b, compare)<<endl; }


설정

트랙백

댓글


mult.m


function C = mult( A, B)

    if(size(A,2)~=size(B,1))

        error('improper matrices size! Retype correct matrices A and B');

    end

   

    temp=zeros(size(A,1),size(B,2));

    sum=0;

    for i=1:size(A,1)

       for j=1:size(B,2)

            sum=0;

            for k=1:size(A,2)

                sum=sum+A(i,k)*B(k,j);

            end

            temp(i,j)=sum;           

       end

    end

    

    C=temp;

end

설정

트랙백

댓글

General Linear List

전전컴/C++ 2013. 10. 16. 22:38





---------------------------------code-------------------------------------------
 
#include <iostream>
using namespace std;

typedef struct tListNode{
	int data;
	struct tListNode *link;
}ListNode;

typedef struct{
	int count;
	ListNode* head;
	ListNode* pos;
}List;


List* createList(){
	List* p = (List*)malloc(sizeof(List));
	p->count = 0;
	p->head = NULL;
	return p;
}

void destroyList(List* p){
	ListNode *pDel=NULL, *pNext=NULL;
	
	if(p->count<=0){
		free(p);
		return;
	}

	for(pDel=p->head; pDel!=NULL; pDel = pNext){
		pNext = pDel->link;
		free(pDel);
		cout<<"del"<<endl;
	}
	free(p);
}

bool searchList(List* p, ListNode** pPre, ListNode** pLoc, int item){
	for( (*pPre)=NULL, (*pLoc)=p->head ; (*pLoc)!=NULL; ){
		if( (*pLoc)->data == item){
			return true;
		}else if( (*pLoc)->data > item ){
			break;
		}
			(*pPre)=(*pLoc);
			(*pLoc)=(*pLoc)->link;
	}
		
	
		return false;
}

void insertList(List* p, ListNode* pPre, int item){
	ListNode *q = (ListNode*)malloc(sizeof(ListNode));
	q->link = NULL;
	if(pPre == NULL){
		p->head = q;
		q->data = item;
	}else{
		q->link = pPre->link;
		pPre->link = q;
		q->data = item;
	}

	p->count++;
}

void addNodeList(List* p, int item){
	ListNode *pPre = NULL, *pLoc = NULL;

	bool found = searchList(p, &pPre, &pLoc, item);

	if(!found){
		insertList(p, pPre, item);
	}
}
void deleteList(List* p, ListNode *pPre, ListNode *pLoc){
	if(pPre == NULL){
		p->head = pLoc->link;
	}else{
		pPre->link = pLoc->link;
	}
	
	free(pLoc);
	p->count--;
}

void removeList(List* p,  int item){
	ListNode *pPre, *pLoc;
	bool found = searchList(p, &pPre, &pLoc, item);

	if(found){
		deleteList(p,pPre,pLoc);
	}
}

void display(ListNode *head){
	ListNode *p;
	if(head ==NULL) return;
	p=head;
	do{
		cout<<p->data<<endl;
		p=p->link;
	}while(p!=NULL);
}

bool traverseList(List* p, int fromWhere, int* data){
	if( p->pos == NULL || fromWhere ==0 ){
		p->pos=p->head;
	}else{
		p->pos = p->pos->link;
	}

	if(p->pos != NULL){
		*data = p->pos->data;
		return true;
	}else{
		*data = 0;
		return false;
	}
	

}

void main(){
	List *p = createList();
	addNodeList(p, 10);
	addNodeList(p, 20);
	addNodeList(p, 30);

	bool check = false;
	int n=0;
	int data;
	
	cout<<"traverseList 함수를이용한display부분"<<endl;
	do{
		check = traverseList(p,n++,&data);
		if(check) cout<<data<<endl;
	}while(check);


	removeList(p,20);
	cout<<"display함수를이용한display 부분"<<endl;
	display(p->head );
	destroyList(p);
}


설정

트랙백

댓글


아래의 실행결과는 main함수에서 포인터를 함수로 넘기고 난후 포인터에 저장되어있는 주소값을 확인한 것이다. 이중포인터를 사용하지 않을 경우에는 함수호출시 단순히 포인터의 주소를 복사한후에 호출된 함수에서 복사한 것으로이루어지기에, main함수내의 포인터의 주소는 바뀌질 않는다.

자료 구조 등에서 함수를 호출할때 call by reference 방식과 같이 input으로 넣어준 포인터의 주소를 이용한후에 다시 그 포인터에 저장된 주소를 바꾼후, main함수내에서도 바뀌어있길 바란다면 이중포인터를 이용한다.

아 글로 쓰려니까 복잡네 그림으로 그리든가 해야지 원 





--------------------------------code-----------------------------------------
#include <iostream>
#include <stdio.h>
using namespace std;

void function(int* a){
/*main함수에서 function함수 호출시 함수에 input으로 넣은 포인터의 주소를 a에 저장함
input으로 넣은 포인터의 주소를 int* a에 저장하는것이므로 a를 바꾼다해서 main 함수의 포인터 p 주소가 바뀌질 않음
이문제를 해결하기위해 아래의 이중포인터를 사용한다.*/
	int c;
	int* b=&c;
	a=b;
}

void main(){
	int a=10;
	int *p=&a;

	cout<<p<<endl;
	function(p);
	cout<<p<<endl;
}





이중포인터를 쓰고 난 결과



------------------code-----------------------
#include <iostream>
#include <stdio.h>
using namespace std

void function(int** a){
	int c;
	int* b=&c;
	*a=b;
}

void main(){
	int a=10;
	int *p=&a;

	cout<<p<<endl;
	function(&p);
	cout<<p<<endl;
}


설정

트랙백

댓글

STL deque

전전컴/C++ 2013. 10. 16. 01:28



 -----------------------------code-------------------------
 
#include <iostream>
#include <stdio.h>
#include <deque>
using namespace std

void main(){
	deque<int> dq

	dq.insert(dq.begin(),5);
	dq.insert(dq.end(),15);
	dq.insert(dq.end(),20);
	dq.erase(dq.begin());
	
	cout<<dq.front()<<endl
	for(int i=0 i< dq.size(); i++){
		cout<<dq[i]<<endl
	}
}


설정

트랙백

댓글