I want to add between large numbers, but it gives an error. I am using a char array. There is an error on line 55. [Error] subscripted value is neither array nor pointer nor vector
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
int chrtoint(char a){
int i;
for (i = 48; i<=57; i )
if (toascii(i)==a) return i-48;
return 0;
}
void main(){
char n1[80];
char n2[80];
printf("Enter First Number:");
scanf("%s", &n1);
printf("\nEnter Second Number:");
scanf("%s", &n2);
sumNumber(n1 , n2);
}
void sumNumber(char n1 , char n2){
int rs[80];
int c1, c2;
int i,j,m, cmax, sum;
c1 = strlen(n1);
c2 = strlen(n2);
strrev(n1);
strrev(n2);
cmax = c1;
if(c1<c2){
cmax = c2;
}
m=0;
for(i=0; i< cmax; i ){
if(c1==c2 || (i < c1 && i < c2)){
sum = m chrtoint(n1[i]) chrtoint(n2[i]); // error is given in this line
}else if(i >=c1){
sum = m chrtoint(n2[i]);
}else if(i >=c2){
sum = m chrtoint(n1[i]);
}
rs[i] = sum;
m = sum/10;
}
if(m){
rs[i]=m;
i ;
}
printf("\nResult: ");
for(j=0; j < i; j ){
printf("%d", rs[i-j-1]);
}
}
I apologize if I have asked the same question. This is the first time I am posting a question on this site. Please tell me if there is something wrong.
CodePudding user response:
Your compiler should be displaying several warning and error messages.
First, either add a prototype to the function void sumNumber(char n1, char n2)
or move it above main
. Speaking of which, your main
function prototype is not good practice (in some compilers it is illegal); it should be int main(void)
or int main(int argc, char *argv[])
.
Here:
void sumNumber(char n1, char n2)
These variables are declared in your main function as arrays of characters so the parameters of sumNumber
should be either arrays of characters:
void sumNumber(char n1[], char n2[])
or pointers to characters:
void sumNumber(char *n1, char *n2)
One more thing, in these lines:
scanf("%s", &n1);
scanf("%s", &n2);
you are passing pointer to an array of 80 characters, which is invalid; it should be
scanf("%s", n1);
scanf("%s", n2);
Correct these errors and your program should run fine.
CodePudding user response:
when we define array of char to be used as string ( we don't scan it with address ) because String in most programing language is already considered as an address (reference pointer )