[ create a new paste ] login | about

Link: http://codepad.org/7IMBDZ2Y    [ raw code | output | fork ]

C, pasted on Jan 6:
#include <stdio.h>
#include <conio.h>
#include<stdlib.h>

void CapPhat(int*& a, int n)  // Cách 1 dùng tham chiếu
{
	//a = new int[n];
	a = (int*)malloc(n * sizeof(int));
}

int* CapPhat(int n)   // Cách 2 Không thay đổi trực tiếp tham số mà trả về
{
	int* a = new int[n];
	//int* a = (int*)malloc(n * sizeof(int));
	return a;
}
void CapPhat(int** p,int n)   // Cách 3 Sử dụng con trỏ p trỏ đến con trỏ a này. Hàm sẽ thay đổi giá trị của con trỏ a gián tiếp thông qua con trỏ p.
{
	//*p = new int[n];
	int* a = (int*)malloc(n * sizeof(int));
}

int main()
{
	int n = 5;
	int* a = CapPhat(n);

	//CapPhat(a, n);      // Sử dụng hàm truyền đối số kiểu tham chiếu
	


	//CapPhat(&a,n);        // Sử dụng hàm với tham số là con trỏ trỏ cấp n + 1
	if(a == NULL)
		printf("Cap phat thai bai!");
	else 
	{
			printf("Cap phat thanh cong");
			delete []a;
	}
	getch();
	return 0;
}


Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Line 18: error: conio.h: No such file or directory
Line 5: error: expected ';', ',' or ')' before '&' token
In function 'CapPhat':
Line 13: error: 'new' undeclared (first use in this function)
Line 13: error: (Each undeclared identifier is reported only once
Line 13: error: for each function it appears in.)
Line 13: error: expected ',' or ';' before 'int'
t.c: At top level:
Line 18: error: conflicting types for 'CapPhat'
Line 12: error: previous definition of 'CapPhat' was here
In function 'main':
Line 26: warning: passing argument 1 of 'CapPhat' makes pointer from integer without a cast
Line 26: error: too few arguments to function 'CapPhat'
Line 38: error: 'delete' undeclared (first use in this function)
Line 38: error: expected expression before ']' token


Create a new paste based on this one


Comments: