-2

this is my first C language coding. It is for my coursework and I have some troubles.

  1. I want to pass a char[] as a parameter to a method
  2. I want to copy this char[] to another char[]. For this I am using strcpy.

So when I do:

main(){
char asd[20] = {"asd"};
insert(asd);
}

void insert(char value[]) //here value[] contains only 'd'
{
   ...code...
}

So basically, it is passing just one char, not the array. I tried with:

main(){
  char *asd[20] = {"asd"};
  insert(asd);
}

void insert(char *value[]) //here value[] contains 'asd'
{
   char *secondArray[20] = {'   '}
   strcpy(secondArray,value); // char** incompatible with "const char*"
}

And I am stucked.

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • I think you should use pointers. – metarose Nov 24 '17 at 13:58
  • 4
    "this is my first (and I hope last) C language coding." Translates for me into: Whatever you teach me I will forget ASAP once I passed the course. I would edit the wording if I were you. – Scheff's Cat Nov 24 '17 at 13:59
  • It should be `int main(void)`, not `main()`. Implicit `int` hasn't been valid C since 1999. – melpomene Nov 24 '17 at 14:06
  • `here value[] contains only 'd'` is nonsense. For one, `value[]` is not a valid expression; second, `value` cannot contain `'d'` because `value` is not a `char`. – melpomene Nov 24 '17 at 14:07
  • Scheff I am writing java and C#, but C is disgusting. I am programming at C just because of school. That's why I am hoping to be my last ;) And thank you for negative vote. – Stiliyan Koev Nov 24 '17 at 14:21

1 Answers1

0

You have not declared your strings correctly, they should be char arrays not array for char pointers

char *asd[20] = {"asd"};

to

char asd[20] = {"asd"};

and

 char *secondArray[20] = {'   '}

to

 char secondArray[20] = {'   '}

Program after corrections:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void insert(char *value);

int main(){
  char asd[20] = {"asd"};
  insert(asd);
  return 0;
}

void insert(char *value) //here value[] contains 'asd'
{
   char secondArray[20] = {"   "};
   strcpy(secondArray,value); // char** incompatible with "const char*"
   printf("%s",secondArray);
   return;
}
Pras
  • 4,047
  • 10
  • 20