PDA

Tam Sürümünü Görmek İçin : Warning sorunu...


aSii_GeNc
14/08/2007, 11:02
Merhaba
Yeni C ye başlayan biriyim.Pek terimleri hakkında bilgim yok.
Aşağıdaki kodn bir struct içindeki bir listeki sıralamaya yarıyor.
Çalışıyor fakat neden warning veriyor çözemedim yardımcı olurmusunuz?

Yazdığım kodlar:

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

#ifndef QSORT_CAST
#define QSORT_CAST chan_cmp
#endif /* QSORT_CAST */

struct ccounts {
char *chan;
unsigned long int *cnt;
};

struct ccounts kanallar[] =
{
{"abcdef2", 10},
{"aabcdef", 20},
{"aabcdef45", 5}
};

int ccount_c = sizeof(kanallar) / sizeof(struct ccounts);
int print_chan(struct ccounts *c);

int chan_cmp (const struct ccounts *c1, const struct ccounts *c2) {
if (c1->cnt > c2->cnt)
return 1;
else if (c1->cnt < c2->cnt)
return -1;
else
return 0;
}

int main () {
int i;
printf("Siralamadan once\n");
for (i = 0; i < ccount_c; i++)
print_chan(&kanallar[i]);

qsort(kanallar, ccount_c,sizeof(struct ccounts), QSORT_CAST);
printf("Siralamadan sonra\n");
for (i = 0; i < ccount_c; i++)
print_chan(&kanallar[i]);

}

int print_chan(struct ccounts *c) {
printf("%s d %d\n",c->chan, c->cnt);
}




Aldığım Uyarı mesajları:

dmin@pardus denemelerin $ gcc -o deneme deneme.c
deneme.c:16: warning: initialization makes pointer from integer without a cast
deneme.c:17: warning: initialization makes pointer from integer without a cast
deneme.c:18: warning: initialization makes pointer from integer without a cast
deneme.c: In function `main':
deneme.c:39: warning: passing arg 4 of `qsort' from incompatible pointer type
admin@pardus denemelerin $ ./deneme
Siralamadan once
abcdef2 d 10
aabcdef d 20
aabcdef45 d 5
Siralamadan sonra
aabcdef45 d 5
abcdef2 d 10
aabcdef d 20
admin@pardus denemelerin $


quasimodo
14/08/2007, 14:17
struct ccounts {
char *chan;
unsigned long int *cnt;
};

struct ccounts kanallar[] =
{
{"abcdef2", 10},
{"aabcdef", 20},
{"aabcdef45", 5}
};

pointerlar yanlizca 0 integeri ile initialize edilebilir....

aSii_GeNc
14/08/2007, 15:58
quasimodo: Dediğin gibi oldu qsort olayınıda aşağıdaki gibi çözdüm. Ama nasıl olduğunu pek anlamadım. Google bana bunu buldu. Şu işaretçiler falan aklımı karıştırıyor. Biri neden olduğunu anlatabilirmi en kısasındır.


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


struct ccounts {
char *chan;
unsigned long int cnt;
};

struct ccounts kanallar[] =
{
{"abcdef2", 10},
{"aabcdef", 20},
{"aabcdef", 28},
{"aabcdef", 22},
{"aabcdef", 25},
{"aabcdef", 24},
{"aabcdef", 26},
{"aabcdef", 29},
{"aabcdef45", 5}
};

int ccount_c = sizeof(kanallar) / sizeof(struct ccounts);
int print_chan(struct ccounts *c);

int chan_cmp (c1,c2)
struct ccounts *c1,*c2;
{
if (c1->cnt > c2->cnt)
return 1;
else if (c1->cnt < c2->cnt)
return -1;
else
return 0;
}


int main () {
int i;
printf("Siralamadan once\n");
for (i = 0; i < ccount_c; i++)
print_chan(&kanallar[i]);

qsort(kanallar, ccount_c,sizeof(struct ccounts), chan_cmp);
printf("Siralamadan sonra\n");
for (i = 0; i < ccount_c; i++)
print_chan(&kanallar[i]);


}

int print_chan(struct ccounts *c) {
printf("%s d %d\n",c->chan, c->cnt);
}

acehreli
14/08/2007, 19:20
cnt'nin turunu bastan yanlis tanimlamistin; duzeltince uyarilar gitmis. :) Ilk degerlerini 10 20 gibi degerlerle verdigine bakarak zaten tamsayi istedigin anlasiliyordu.

quasimodo, isaretcilere 0'dan farkli ilk degerler de verilebilir. Bak burada bir kac tane ornek var:


typedef struct
{
int * isaretci;
} Yapi;

int main()
{
int sayi0 = 42;
int sayi1 = 7;

Yapi dizi[] = {
{ &sayi0 },
{ &sayi1 }
};

double kesirli = 5;
double * dp = &kesirli;

long birDonanimAdresi = 0xa0000000;
short * adresim = (short*)birDonanimAdresi;

return 0;
}


Ali

quasimodo
14/08/2007, 23:10
integer olarak yanlizca sifir demek istemistim ben :P

acehreli
14/08/2007, 23:14
Bir seyi yanlis anladigimdan supheleniyordum zaten :)

Ali

aSii_GeNc
20/08/2007, 14:43
Teşekkür ederim quasimodo ve acehreli...