Usuwanie tablicy przez funckje

0

Witam. Otóż posiadam taki program który tworzy, wczytuje, wypisuje i usuwa tablice dynamiczną. Do każdego zadania przypisana jest odpowiednia funkcja, aczkolwiek waHam się co do tej ostatniej, a mianowicie usuwania. Program się kompiluje lecz nie wiem dlaczego moja tablica nie jest usuwana z pamięci. Proszę o pomoc. Pozdrawiam

 
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

int tworz(int n);
void wczytaj(int *tab,int n);
void drukuj(int *tab,int n);
void usun(int *tab);
int main(int argc, char *argv[])
{
  int n;
  int *tab;
  int wybor;
  printf("Podaj wielkosc tablicy");
  scanf("%d",&n);
  do
  {
  scanf("%d",&wybor);
  switch(wybor)
  {
  case 1: 
       *tab=tworz(n); 
       break;
  case 2: 
       wczytaj(tab,n); 
       break;
  case 3: 
       drukuj(tab,n); 
       break;
  case 4: 
       usun(*tab); 
       break;
}
}while(wybor);
  system("PAUSE");	
  return 0;
}
int tworz(int n)
{
    return (int)calloc(n,sizeof(int));
}

void wczytaj(int *tab,int n)
{
     int i;
     for(i=0;i<n;++i)
     {
     tab[i]=rand()%20;
     }
}
void drukuj(int *tab,int n)
{
     int i;
     for(i=0;i<n;++i)
     {
                     printf("%d element jest rowny: %d\n",i+1,tab[i]); 
     }
}
void usun(int *tab)
{
     printf("Usunales tablice\n");
     free(tab);
}
0

Przekazujesz obiekt a nie tablicę a dokładniej obiekt tab[0]

jak masz funkcję usun to prześlij do niego wskaźnik na tablicę (bez wyłuskania), mianowicie
case 4 : usun(tab);

0

Tworzenie też jest złe, powinno być

int* tworz(int n)
{
    return (int*)calloc(n,sizeof(int));
}

//wywołanie
tab = tworz(n);

BTW. Nie wiem w czym kompilujesz, ale VC++ nie skompiluje tego Twojego kodu. Właśnie przez złe wywołanie usun

0
byku_guzio napisał(a)

Tworzenie też jest złe, powinno być

int* tworz(int n)
{
    return (int*)calloc(n,sizeof(int));
}

//wywołanie
tab = tworz(n);

BTW. Nie wiem w czym kompilujesz, ale VC++ nie skompiluje tego Twojego kodu. Właśnie przez złe wywołanie usun

Teraz się w ogóle nie kompiluje, a pracuje w VS 2010 ;)

Error list :
Error 3 error C2440: '=' : cannot convert from 'int *' to 'int' c:\users\artur\documents\visual studio 2010\projects\1\1\1.cpp 22
Error 4 error C2664: 'usun' : cannot convert parameter 1 from 'int' to 'int *' c:\users\artur\documents\visual studio 2010\projects\1\1\1.cpp 31
Error 5 error C2109: subscript requires array or pointer type c:\users\artur\documents\visual studio 2010\projects\1\1\1.cpp 48
Error 6 error C2100: illegal indirection c:\users\artur\documents\visual studio 2010\projects\1\1\1.cpp 56
Error 7 error C2664: 'free' : cannot convert parameter 1 from 'int' to 'void *' c:\users\artur\documents\visual studio 2010\projects\1\1\1.cpp 62
8 IntelliSense: a value of type "int *" cannot be assigned to an entity of type "int" c:\users\artur\documents\visual studio 2010\projects\1\1\1.cpp 22
9 IntelliSense: argument of type "int" is incompatible with parameter of type "int " c:\users\artur\documents\visual studio 2010\projects\1\1\1.cpp 31
10 IntelliSense: expression must have pointer-to-object type c:\users\artur\documents\visual studio 2010\projects\1\1\1.cpp 48
11 IntelliSense: operand of '
' must be a pointer c:\users\artur\documents\visual studio 2010\projects\1\1\1.cpp 56
12 IntelliSense: argument of type "int" is incompatible with parameter of type "void *" c:\users\artur\documents\visual studio 2010\projects\1\1\1.cpp 62

0

Bo u Ciebie jest:
*tab = tworz(n)
a u Guzia jest:
tab = tworz(n)

0
MJay napisał(a)

Bo u Ciebie jest:
*tab = tworz(n)
a u Guzia jest:
tab = tworz(n)

Mój kod po zmianach, błędy podobne.

 int* tworz(int n);
void wczytaj(int *tab,int n);
void drukuj(int tab,int n);
void usun(int tab);
int main(int argc, char *argv[])
{
  int n;
  int tab;
  int wybor;
  printf("Podaj wielkosc tablicy");
  scanf("%d",&n);
  do
  {
  scanf("%d",&wybor);
  switch(wybor)
  {
  case 1: 
       tab=tworz(n); 
       break;
  case 2: 
       wczytaj(tab,n); 
       break;
  case 3: 
       drukuj(tab,n); 
       break;
  case 4: 
       usun(tab); 
       break;
}
}while(wybor);
  system("PAUSE");	
  return 0;
}
int* tworz(int n)
{
    return (int*)calloc(n,sizeof(int));
}

void wczytaj(int *tab,int n)
{
     int i;
     for(i=0;i<n;++i)
     {
     *tab[i]=rand()%20;
     }
}
void drukuj(int *tab,int n)
{
     int i;
     for(i=0;i<n;++i)
     {
                     printf("%d element jest rowny: %d\n",i+1,*tab[i]); 
     }
}
void usun(int tab)
{
     printf("Usunales tablice\n");
     free(tab);
}
0

Wszędzie tam, gdzie masz *tab[i] usuń gwiazdki

0

I tak są błędy w usun(), wczytaj(), drukuj(). Poprawiony (VS2010 Express):

#include <memory>

int* tworz(int n);
void wczytaj(int* tab, int n);
void drukuj(int* tab, int n);
void usun(int* tab);

int _tmain(int argc, _TCHAR* argv[])
{
	int n;
	int* tab;
	int wybor;

	printf("Podaj wielkosc tablicy");
	scanf("%d",&n);
	
	do
	{
		printf("Wybierz opcje: 1-tworz, 2-wczytaj, 3-drukuj, 4-usun");
		scanf("%d", &wybor);
		switch(wybor)
		{
		case 1: 
			tab=tworz(n); 
			break;
		case 2: 
			wczytaj(tab, n); 
			break;
		case 3: 
			drukuj(tab, n); 
			break;
		case 4: 
			usun(tab); 
			break;
		}
	}
	while(wybor);
     
	return 0;
}

int* tworz(int n)
{
    return (int*)calloc(n, sizeof(int));
}
 
void wczytaj(int* tab, int n)
{
     int i;
     for(i=0; i<n; ++i)
     {
		tab[i] = rand() % 20;
     }
}
void drukuj(int *tab, int n)
{
     int i;
     for(i=0; i<n; ++i)
     {
		printf("%d element jest rowny: %d\n", i+1, tab[i]); 
     }
}
void usun(int* tab)
{
     printf("Usunales tablice\n");
     free(tab);
}
0
#include <stdio.h>
#include <stdlib.h>

Więcej błędów raczej nie ma

1 użytkowników online, w tym zalogowanych: 0, gości: 1