Thursday, 15 January 2015

making function like strcmp() in C -



making function like strcmp() in C -

am making function strcmp() witch finds name in 2d array of names if name there returns 0 , else returns -1 , problem when utilize code returns 0 when type first element of arrayofnames.

int my_strcmp(char arrayofnames[5][20], char nametofind[20]) { int i,j; (j=0;j<5;j++) { (i = 0; arrayofnames[j][i] &&nametofind[i]; ++i) { if (arrayofnames[j][i] == nametofind[i]) continue; else break; } if (arrayofnames[j][i] == nametofind[i]) homecoming 0; else //set 6th bit in both, compare homecoming -1; } }

assuming array pass dimensions 5,20 have error in first loop:

for (j=0;j<=5;j++)

where go out of bounds

for (j=0;j<5;j++)

is right loop

however comment //set 6th bit in both, compare indicates have larger dimensions specified.

the improve way form function specify dimensions additional parameters , pass additional array storing results.

c

No comments:

Post a Comment