-3

I'm trying to declare a char* using dynamic memory allocation then initialize it with a string of characters, but I keep getting this error...

Error Message:

a value of type "const char *" cannot be used to initialize an entity of type "int *"

Code:

void main() {
    char *alphabet = new char();
    alphabet = "abcdefghijklmnopqrstuvwxyz";
    cout << alphabet;

    system("pause");
}


I understand that this

void main() {
    char *alphabet = new char();
    cin.getline(alphabet, 255);
    cout << alphabet;

    system("pause");    
}

will work, but I don't understand why I can't initialize it without user input;

FMT_WL
  • 15
  • 4
  • 8
    No, that does not work, either. You seem to have quite a few misunderstandings about C++. You should take a step back and systematically learn the language from a good book. – Baum mit Augen Jun 11 '18 at 12:06
  • 3
    `new char()` allocate space for *one single character* and initializes it to zero. Then in the first program you reassign `alaphabet` to point to a constant string literal (which is of type `const char *`) which makes you lose the original pointer returned by `new`. And nowhere in either program do you have any `int *` which means the error you show is not from any of the programs you show. – Some programmer dude Jun 11 '18 at 12:09
  • 2
    Which compiler is this? Both GCC and Clang treat `void main` as an error. – Michael Jun 11 '18 at 12:10
  • And please take some time to read [the help pages](http://stackoverflow.com/help), take [the SO tour](http://stackoverflow.com/tour), read about [how to ask good questions](http://stackoverflow.com/help/how-to-ask), and learn how to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). I also recommend you read [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/), and all of http://idownvotedbecau.se/ to learn some reasons for your negative votes. – Some programmer dude Jun 11 '18 at 12:10
  • 1
    Lastly, if you really want to learn programming and C++, please [get some good books to read](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282). – Some programmer dude Jun 11 '18 at 12:11
  • Ahhhhh, Okay, I need to know more about the basic, guess I didn't learn enough from the study. – FMT_WL Jun 11 '18 at 12:18
  • 1
    See [The Definitive C++ Book Guide and List](//stackoverflow.com/a/388282) as linked above. – Baum mit Augen Jun 11 '18 at 12:19
  • @Michael i was using visual studio 2017. – FMT_WL Jun 11 '18 at 12:21
  • Thanks @BaummitAugen and Some programmer dude for the feedback, guess I got lots of reading ahead of me. this is still too new for me i guess. – FMT_WL Jun 11 '18 at 12:25
  • @Ron Yea, i know it would be a lot easier to use std::string, but I was sort of trying to understand more about char* or how I can initialize variables in c++. – FMT_WL Jun 11 '18 at 12:27

1 Answers1

0

Your question is not good. But I still give use a question. you can try as below:

#include<iostream>

using namespace std;
void showAlphabetArray(char*alp)
{
  for(int i=0;i<26;i++)
  {
    cout<<alp[i];
  }
}
int main() {
  //declare and init `char` array
  char *alphabet = new char[26];
  //init value
  for(int i=0;i<26;i++)
  {
      alphabet[i] = i + 97;
  }
  showAlphabetArray(alphabet);
  system("pause");    
}

You should reference to ASCII https://en.wikipedia.org/wiki/ASCII

TaQuangTu
  • 2,155
  • 2
  • 16
  • 30