Re: Why this doesn't work



On Fri, 31 Dec 2010 01:41:07 -0800 (PST), Milutin_Popovski
<milutin.popovski@xxxxxxxxx> wrote:

It would be nice if you told us why you think it is not working.

//It occures is a number palindrom,
//For example 1221 is plandrom, and 1231 is not...

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

struct lista{
int val;
struct lista *next;
struct lista *prev;
};

int main(int argc , char *argv[]){
int i,j, k, n, m;
struct lista *first, *temp, *temp_prev, *old, *last;
if(argc!=2 || !chk(argv[1])){
printf("\nWrong argument");
return 1;
}
m=atoi(argv[1]);
first=malloc(sizeof(struct lista));
temp=first;
old=first;
temp->next=NULL;
temp->prev=NULL;
old->prev=NULL;

How many times do you need to set the same object to NULL?

n=m;
while(m>0){
temp->val=m%10;
old=temp;
temp->next=malloc(sizeof(struct lista));
temp=temp->next;
temp->next=NULL;
temp->prev=old;
m=m/10;
}
temp=first;
printf("\n======================\n");
while(temp->next){
printf("%d ", temp->val);
temp=temp->next;
}
temp=temp->prev;
last=temp;
printf("\n======================\n");
while(temp){
printf("%d ", temp->val);
temp=temp->prev;
}
temp=first;
temp_prev=last;
while(temp){
if(temp->val!=temp_prev->val) goto izlaz;

There is an "else" construct in the language which eliminates the need
for an unstructured goto like this.

printf("\n<%d><%d>", temp->val, temp_prev->val);
temp=temp->next;
temp_prev=temp_prev->prev;
}
printf("\n%d is palindrom", n);
return 0;
izlaz:
printf("\n%d is not a palindrom", n);

return 0;
}



int chk(char *c){
if(!isdigit(*c) && *c!='\0') return 0;
if(*c=='\0') return 1;
*c++;

Why are you bothering to dereference the pointer and then throw the
resulting char away?

chk(c);

You call the function recursively yet you ignore the value it returns.
What value will get passed back to main if the first character is a
valid digit?

}

--
Remove del for email
.