Mam taki kod
#include <iostream>
#include <fstream>
using namespace std;
const int buffSize = 1024;
int main()
{
char buff[buffSize];
char **explode, explodeChar = ' ';
int count = 0, explodeIndex = 0, tmpIndex = 0;
fstream file("dane.txt", ios::in);
file.getline(buff, buffSize);
for (int i = 0; i < strlen(buff); i++)
if (buff[i] == explodeChar) count++; // zliczamy ilość separatorów
explode = new char*[count + 1]; // dynamiczna alokacja potrzebnej pamięci
for (int i = 0; i < count + 1; i++)
explode[i] = new char[buffSize];
for (int i = 0; i < strlen(buff); i++) // proces separowania
{
if (buff[i] != explodeChar)
{
explode[explodeIndex][tmpIndex] = buff[i];
tmpIndex++;
}
else
{
explode[explodeIndex][tmpIndex] = '\0';
explodeIndex++;
tmpIndex = 0;
}
}
for (int i = 0; i < count + 1; i++) // wypisujemy na ekran
cout << explode[i] << endl;
file.close();
for (int i = 0; i < count + 1; i++) // sprzatamy po sobie
delete [] explode[i];
delete [] explode;
getchar();
return 0;
}
Chciałem powyzszy kod w C++ przerobić na C ale wysypuje sie błąd w linijce:
explode = (char) malloc (count+1);*
#include <stdio.h>
#include <string.h>
int main()
{
char **explode,explodeChar = ',';
int count = 0, explodeIndex = 0, tmpIndex = 0, i = 0;
const char *string="a,abc,abcd,abcde,abcdef,abcdefg";
const char a[50]="a,abc,abcd,abcde,abcdef,abcdefg";
for (i = 0; i < strlen(a); i++)
if (a[i] == explodeChar) count++;
explode = (char*) malloc (count+1);
for (i = 0; i < strlen(a); i++) // proces separowania
{
if (a[i] != explodeChar)
{
explode[explodeIndex][tmpIndex] = a[i];
tmpIndex++;
}
else
{
explode[explodeIndex][tmpIndex] = '\0';
explodeIndex++;
tmpIndex = 0;
}
}
for (i = 0; i < count + 1; i++) // wypisujemy na ekran
printf("Dane: %s\n",explode[i]);
printf("Dane: %s \n",a);
return 0;
}
Info Error:
# gcc -o test test.c -lpq
test.c: In function 'main':
test.c:15: warning: incompatible implicit declaration of built-in function 'malloc'
test.c:15: warning: assignment from incompatible pointer type