0

I have a array called Table1 witch contains '1','2','3','4' I do not want to work with global variables I want to change the content of Table1 with a function called 'Change' that gives a return char array back in to Table1

This is the program i wrote:

void setup()

{
  Serial.begin(9600);
}

void loop()
{
  char tabel1[4] = {'1','2','3','4'};
  int i;
  for (i = 0; i <= 3; i++)
  {
    Serial.println(tabel1[i]);
  }

  tabel1 = Change(tabel1);

  for (i = 0; i <= 3; i++)
  {
    Serial.println(tabel1[i]);
  }
}

char Change(char tabel2)

{
  tabel2[0] = 'n';
  tabel2[1] = 'b';
  return tabel2;

}

This is the error i'm getting:

In function 'void loop()':
testArray:17: error: incompatible types in assignment of 'char' to 'char [4]'
tabel1=Change(tabel1);
^
In function 'char Change(char*)':
30:12: warning: invalid conversion from 'char*' to 'char' [-fpermissive]
return tabel2;
^
exit status 1

[Error] Exit with code=1
  • Did you mean table or tabel?? – Dat Ha Jun 18 '17 at 12:30
  • I'm dutch and table in dutch is tabel so yeh XD – Cyber_Star Jun 20 '17 at 21:55
1

You should read something about C arrays and how they work.

Anyway, if you use:

  Change(tabel1);

// ...

void Change(char * tabel)
{
  tabel[0] = 'n';
  tabel[1] = 'b';
}

It'll change passed array because it's passed as pointer to the first element of tabel1.

1

In c an array's name is effectively a pointer to the start of the memory location where the array is stored. You have to be careful, if you pass an array to a function that function will have no knowledge of the array size. If there is a risk of the size being unknown you normally pass a second parameter, the size of the array. In this case we will assume the array passed to change is always at least 2 chars long and risk a crash if it isn't.

At the most simple level your change function becomes:

void Change(char tabel[])
{
  tabel[0] = 'n';
  tabel[1] = 'b';
}

and the line to call it is: Change(tabel1);

Notice how there is no return type or return value. We were passed a pointer to where the array is in memory. When we make changes to the values we are changing the original array directly. The old values are lost.

The function:

void Change(char *tabel)
{
  *tabel = 'n';
  *(tabel+1) = 'b';
}

Is functionally identical to the one above only it is explicitly using a pointer rather than an array.

One thing you need to get used to in c is that an array is not a type of variable, it's a collection of variables. As such normal variable operations don't work on them.

e.g. array1 = array2 does not set the contents of array 1 to being the same as array2. It sets the memory address where array1 is stored to being the same as the memory address that is used for array2. The original memory used to store array1 is no longer accessible by the program and is now unusable. This is a bad thing.
Similarly if (array1 == array2) doesn't compare the contents of the two arrays, it compares their memory addresses.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.