sundeepgopal
asked on
returning a string from a function
Hi experts,
I want to send a string to function and after using that it should return a string to the main function. I was not able to do that. Could you guys please help me ???
When i trace it , the flow control never goes to second function (i.e., opcodes).
#include <string.h>
#include <stdio.h>
void main()
{
char *opcodes(char* );
char *s;
s=opcodes("100011");
printf("%s",s);
}
char *opcodes(char a)
{
int i;
char *opco[][2]=
{
{"SUB", "100010"},
{"SUBU", "100011"},
{"ADD", "100000"},
{"ADDU", "100001"},
{"AND", "100100"},
{"OR", "100101"}, //R Instructions where opcode="000000" and functions codes are here
{"NOR", "100111"},
{"XOR", "100110"},
{"SLT", "101010"},
{"SLL", "000000"},
{"SRL", "000010"},
{"SRA", "000011"},
{"JR", "001000"},
{"BEQ", "000100"},
{"BNE", "000101"},
{"BGEZ", "000001"}, //BGEZ and BLTZ have same opcode but rt's are different
{"BGTZ", "000111"},
{"BLEZ", "000110"},
{"BLTZ", "000001"}, //I instructions with their opcodes
{"SLTI", "001010"},
{"ADDI", "001000"},
{"ADDIU","001001"},
{"J", "000010"}, //JUMP opcodes
};
for (i=0;i<=22;i++)
{
if(opco[i][1]==a)
return(opco[i][0]);
}
}
I want to send a string to function and after using that it should return a string to the main function. I was not able to do that. Could you guys please help me ???
When i trace it , the flow control never goes to second function (i.e., opcodes).
#include <string.h>
#include <stdio.h>
void main()
{
char *opcodes(char* );
char *s;
s=opcodes("100011");
printf("%s",s);
}
char *opcodes(char a)
{
int i;
char *opco[][2]=
{
{"SUB", "100010"},
{"SUBU", "100011"},
{"ADD", "100000"},
{"ADDU", "100001"},
{"AND", "100100"},
{"OR", "100101"}, //R Instructions where opcode="000000" and functions codes are here
{"NOR", "100111"},
{"XOR", "100110"},
{"SLT", "101010"},
{"SLL", "000000"},
{"SRL", "000010"},
{"SRA", "000011"},
{"JR", "001000"},
{"BEQ", "000100"},
{"BNE", "000101"},
{"BGEZ", "000001"}, //BGEZ and BLTZ have same opcode but rt's are different
{"BGTZ", "000111"},
{"BLEZ", "000110"},
{"BLTZ", "000001"}, //I instructions with their opcodes
{"SLTI", "001010"},
{"ADDI", "001000"},
{"ADDIU","001001"},
{"J", "000010"}, //JUMP opcodes
};
for (i=0;i<=22;i++)
{
if(opco[i][1]==a)
return(opco[i][0]);
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hi sundeepgopal,
>> if(opco[i][1]==a)
This compares the two pointers, not the strings themselves.
Paul
>> if(opco[i][1]==a)
This compares the two pointers, not the strings themselves.
Paul
ASKER
thanks paul
ASKER