5 Replies - 3449 Views - Last Post: 07 May 2011 - 09:35 AM Rate Topic: -----

#1 pmcmahon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 14-January 11

Passing char array, printing result

Posted 06 May 2011 - 11:01 AM

Hi
I'm trying to pass a char array to this function then print whatever it assigned mnemonic (last argument) to be, so something like

char mnemonic[3];
what_op(char array, char x, mnemonic)
printf("%s\n", mnemonic); //this works when place immediately after e.g. mnemonic = "MOV";



This doesn't seem to work - a serious pointer mess occurs, all sorts of things get printed out but not the mnemonic. This might not even be a sensible task in C but if there is an easy fix please let me know!

Thanks in advance for any help.

Function:
int what_op(char opcode[], char  op_char_6, char mnemonic[])
{
  if (opcode[0] == '0' && opcode[1] == '0')
  {
    if (opcode[3] == '1' && opcode[4] == '1' && opcode[5] == '0' &&
      opcode[6] == '1')
    {
      mnemonic = "MOV";
      printf("BUMS: %s\n", mnemonic);
      return 1;
    }
    else  if (opcode[3] == '0' && opcode[4] == '1' && opcode[5] == '0' &&
      opcode[6] == '0')
    {
      mnemonic = "ADD";
      return 2;
    }
    else  if (opcode[3] == '0' && opcode[4] == '0' && opcode[5] == '1' &&
      opcode[6] == '0')
    {
      mnemonic = "SUB";
      return 3;
    }
    else  if (opcode[3] == '1' && opcode[4] == '0' && opcode[5] == '1' &&
      opcode[6] == '0')
    {
      mnemonic = "CMP";
      return 4;
    }
    else  if (opcode[3] == '0' && opcode[4] == '0' && opcode[5] == '0' &&
      opcode[6] == '0')
    {
      if (opcode[0] == '0' && opcode[1] == '0' && opcode[2] == '0' && op_char_6 == '9')
      {
        mnemonic = "MUL";
        return 14;
      }
      mnemonic = "AND";
      return 5;
    }
    else  if (opcode[3] == '0' && opcode[4] == '0' && opcode[5] == '0' &&
      opcode[6] == '1')
    {
      if (opcode[0] == '0' && opcode[1] == '0' && opcode[2] == '0' && op_char_6 == '9')
      {
        mnemonic = "MLA";
        return 15;
      }
      mnemonic = "EOR";
      return 6;
    }
    else  if (opcode[3] == '1' && opcode[4] == '1' && opcode[5] == '0' &&
      opcode[6] == '0')
    {
      mnemonic = "ORR";
      return 7;
    }
    else  if (opcode[3] == '1' && opcode[4] == '1' && opcode[5] == '1' &&
      opcode[6] == '0')
    {
      mnemonic = "BIC";
      return 8;
    }
    else return 0;
  }
  else
  {
    if (opcode[0] == '1' && opcode[1] == '0' && opcode[2] == '1' && opcode[3] == '0')
    {
      mnemonic = "B";
      return 9;
    }
    else if (opcode[0] == '1' && opcode[1] == '0' && opcode[2] == '1' && opcode[3] == '1')
    {
      mnemonic = "BL";
      return 10;
    }
    else if (opcode[0] == '0' && opcode[1] == '1' && opcode[7] == '1')
    {
      mnemonic = "LDR";
      return 11;
    }
    else if (opcode[0] == '0' && opcode[1] == '1' && opcode[7] == '0')
    {
      mnemonic = "STR";
      return 12;
    }
    else if (opcode[0] == '1' && opcode[1] == '1' && opcode[2] == '1' && opcode[3] == '1')
    {
      mnemonic = "SVC";
      return 13;
    }
    else return 0;
  }
}


Is This A Good Question/Topic? 0
  • +

Replies To: Passing char array, printing result

#2 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 943
  • View blog
  • Posts: 2,353
  • Joined: 15-February 11

Re: Passing char array, printing result

Posted 06 May 2011 - 11:14 AM

This ->
what_op(char array, char x, mnemonic)
says that you are trying to pass a char variable as the first parameter.

To pass an array either do this
what_op("characters",.... //the hard-code way
or
char array[] = "characters";
what_op(array,... //simple way
or by reference
char array[] = "characters";
what_op(&array[0],... //the address of the first element

This post has been edited by codeprada: 06 May 2011 - 11:15 AM

Was This Post Helpful? 1
  • +
  • -

#3 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Passing char array, printing result

Posted 06 May 2011 - 12:20 PM

Quote

or by reference...what_op(&array[0],
No. &array[0] is the same as array in that expression (the latter decays to a pointer).

Passing by reference requires a change in the declaration of the function.
Was This Post Helpful? 1
  • +
  • -

#4 pmcmahon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 14-January 11

Re: Passing char array, printing result

Posted 07 May 2011 - 08:35 AM

Thanks, I actually made an error in that post - the first argument is largely irrelevant - I can pass arrays fine it's getting them back that's the problem. I want to get the value of the third argument, which hopefully should be what's assigned to it by the what_op function. Any advice on this?

Thanks
Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is online

  • Saucy!
  • member icon

Reputation: 5951
  • View blog
  • Posts: 23,212
  • Joined: 23-August 08

Re: Passing char array, printing result

Posted 07 May 2011 - 09:18 AM

You have to:

1. provide an array of sufficient size to the function.
2. use strcpy to copy the data into the array. You CANNOT assign strings to character arrays using =.
Was This Post Helpful? 1
  • +
  • -

#6 pmcmahon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 14-January 11

Re: Passing char array, printing result

Posted 07 May 2011 - 09:35 AM

OKay thanks, i just assumed that since when assigning with '=' printf worked it might have been okay.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1