-4

Everything works, No problems with compilation

#include <iostream>
#include <conio.h>

using namespace std;

void main(){
    char chArray[30];
    char chArray2[] = "this is working";
    *chArray = *chArray2;

    cout << chArray;
    getch();
}

But...result is

enter image description here

and I don't know what I did wrong guys. Why is the output as shown. Please help :>

Suvarna Pattayil
  • 5,136
  • 5
  • 32
  • 59
Bartek Maciąg
  • 29
  • 1
  • 1
  • 4

4 Answers4

2

*chArray = *chArray2; will just set the first array element, not the whole "string".

P.S. Didn't go offsite to look at the result. You should describe it in the question.

John3136
  • 28,809
  • 4
  • 51
  • 69
  • you have right but I must have 10 reputation to put picture inside question. Sorry, and one more question What can I do to working copy array ? – Bartek Maciąg Jun 20 '13 at 10:52
2

Ah it's so easy to mistake C for C++...

#include <iostream>
#include <string>

using namespace std;

int main(){
    string a;
    string b = "this is working";
    a = b;

    cout << a;
}

Notes:

  • conio.h/getch aren't standard C++.
  • C++ requires main return type to be int.
Bartek Banachewicz
  • 38,596
  • 7
  • 91
  • 135
  • Dzięki ale potrzebowałem tablic sprawdzałem jak działają, ponieważ potrzebuje ich do budowania applikacji z winsock'iem a tam nie mogę przesyłać stringów ;/ – Bartek Maciąg Jun 20 '13 at 17:25
  • @BartekMaciąg FYI: `string.c_str()`. And please use English, not Polish, so others can understand our discussion, too! :) – Bartek Banachewicz Jun 20 '13 at 21:38
1
char chArray[30];

This creates an array of 30 chars and has a base address which is same as &chArray[0] i.e the address of the 1st element of the array and is the same address that you get when when you do chArray (array name acts as a pointer)

char chArray2[] = "this is working";

You are creating a constant string literal assigning it to chArray2 at the line of declaration. The array has base address which is same as &chArray2[0] and is the same when you do chArray2

*chArray will refer to the 1st element of the array and so will *chArray2

By using * you are assigning the value of 1st element of chArray2 to chArray not the address.

AND you can't/shouldn't do that.

chArray is not a pointer of type char* to which you can assign an address char* p = chArray2, but its of type char(*chArray)[30] . The address is automatically generated.

When you do std::cout it prints garbage because nothing is assigned to chArray yet except the 1st character (note there is no \0 to mark the end of string so while printing you print garbage) *chArray = *chArray2;

You need to use strcpy(chArray,chArray2); to properly copy your chArray2 to chArray

If you are using c++ (as tagged), you can use std::string

Its easier and better.

string chArray;
string chArray2 = "this is working";
chArray = chArray2;

Sidenote:

Make it int main()

Community
  • 1
  • 1
Suvarna Pattayil
  • 5,136
  • 5
  • 32
  • 59
-1

In C/C++ pointer the operator '&'denotes the address and the '*' denotes the value of pointer type.

In your case the code *chArray = *chArray2 assigns the character 't' to *chArray array.

So you are getting junk characters, because the string is not terminated with '\0' character.

Option 1: you can use strcpy function to copy the strings. strcpy(chArray, chArray2);

Option 2: Both the variables shall be declared as pointers and shall be assigned with '=' operator.