A value of type "*const char *" cannot be assigned to an entity of type "char *"

value type in c#
a value of type const char cannot be used to initialize an entity of type char
value types java
value type and reference type in c# stackoverflow
value type and reference type in c# interview questions
reference type in java
a value of type int cannot be used to initialize an entity of type
value type vs reference type swift

So I am trying to avoid using strings for this. I am basically trying to make a string array.

char **hourtimes = (char**)malloc(100 * sizeof(char*));

    for (int i = 0; i < 100; i++) {
        (*hourtimes) = (char*)malloc((100 * sizeof(char)));
    }

So I made a string array basically here

Now, I want to make hourtimes[0] = "twelve";

I tried doing *hourtimes = "twelve";

but I get the same error, I think this works in c, but I'm using c++

hourtimes[0][0] = 't'; hourtimes[0][1] = 'w';

etc works just fine but that would be too cumbersome

*hourtimes = "twelve" is setting *hourtimes to point to a string in Read Only Memory. You are then trying to modify the data stored within that Read Only Memory. What you want to do is copy "twelve" into *hourtimes.

strcpy(hourtimes[0],"twelve");

Note: This answer was written at a time when the question was tagged for C. C++ will have different preferred ways of doing this kind of thing.

A value of type "const char*" cannot be used to initialize an entity of , The Java programming language provides a number of operators that act on integral values: The comparison operators, which result in a value of type boolean :. A value type can be one of the two following kinds: a structure type , which encapsulates data and related functionality an enumeration type , which is defined by a set of named constants and represents a choice or a combination of choices

The error message tells you exactly what's wrong: You can't assign a const char * to a char *. What does that mean, though?

Both const char * and char * are types. They are, in fact, very nearly the same type; one is a pointer to a character, and the other is a pointer to a constant character. That means that the latter can't be changed1; that's, after all, what "constant" means. So when you try to tell the compiler to treat a pointer to a constant type as a pointer to a non-const type, it'll give you an error -- because otherwise it'd have no way to guarantee that the string isn't modified.

"whatever" is always a const char *, not a char *, because that's stored in memory that's generally not meant to be modified, and the compiler can make some really neat optimizations if it can safely assume that it's unchanged (which, because it's const, it can).

I won't tell you how to "properly" write the code you're going for, because if you're using C++, you should be using std::vector and std::string instead of anything with pointers whenever possible, and that probably includes here. If, for whatever reason, you need to use pointers, the comments have covered that well enough.


1: Okay, yes, it can -- but that's outside the scope of this answer, and I don't want to confuse any beginners.

Value types, Type: LanguageService Describe the bug OS and Version: macOS 10.14.3 VS Code Version: 1.13.1 C/C++ Extension Version: 0.21.0 Other  Value types include the following: All numeric data types. Boolean, Char, and Date. All structures, even if their members are reference types. Enumerations, since their underlying type is always SByte, Short, Integer, Long, Byte, UShort, UInteger, or ULong.

In your allocation loop, (*hourtimes) is the same as hourtimes[0], so you are assigning your allocated sub-arrays to the same slot in the main array on each loop iteration, causing memory leaks and uninitialized slots. You need to use hourtimes[i] instead:

char **hourtimes = (char**)malloc(100 * sizeof(char*));

for (int i = 0; i < 100; i++) {
    hourtimes[i] = (char*)malloc(100 * sizeof(char));
}

And don't forget to deallocate the arrays when you are done with them:

for (int i = 0; i < 100; i++) {
    free(hourtimes[i]);
}

free(hourtimes);

Now, a string literal has type const char[N], where N is the number of characters in the literal, + 1 for the null terminator. So "twelve" would be a const char[7].

Your arrays only allow char* pointers to be stored, but a const char[N] decays into a const char* pointer to the first char. You can't assign a const char* to a char*, thus the compiler error.

Even if it were possible to do (which it is, but only with a type-cast), you shouldn't do it, because doing so would cause a memory leak as you would lose your original pointer to the allocated array, and worse free() can't deallocate a string literal anyway.

What you really want to do is copy the content of the string literal into the allocated array storage. You can use strncpy() for that:

strncpy(hourtimes[0], "twelve", 100);

Now, with all of that said, this is the C way of handling arrays of strings. The C++ way is to use std::vector and std::string instead:

#include <string>
#include <vector>

std::vector<std::string> hourtimes(100);
...
hourtimes[0] = "twelve";

Chapter 4. Types, Values, and Variables, I'm having similar issues with structs declared within a class, same errors a value of type "Example *" cannot be assigned to an entity of type  A clear and concise description of what the bug is: A value of type X cannot be used to initialize an entity of type X To Reproduce Create a new document, select the language to be "C++", and type:

This is a string literal, which can be used as a pointer to a constant char, but not as a pointer to a non-const char.

"twelve"

You do however attempt to assign it to a pointer to non-const char.

hourtimes[0] = "twelve";

That is what the compiler does not like.

A value of type X cannot be used to initialize an entity of type X , The typical use is to take a value with static type interface{} and extract its dynamic type information by calling TypeOf, which returns a Type. A call to ValueOf  A value of type "const char *" cannot be used to initialize an entity of type "char *" 1 year, 11 months ago Edited by Lost Astronaut Studios on May 13, 2018, 6:50 a.m.

IntelliSense error a value of type "Example *" cannot be assigned to , There are no type definitions in the language; each value carries its own type. There are eight basic types in Lua: nil, boolean, number, string, userdata, function,  In the first implementation of JavaScript, JavaScript values were represented as a type tag and a value. The type tag for objects was 0. null was represented as the NULL pointer (0x00 in most platforms). Consequently, null had 0 as type tag, hence the "object" typeof return value.

reflect, In computer programming, data types can be divided into two categories: value types and reference types. A value of value type is the actual value. A value of  The errors are: E0167 argument of type "const char *" is incompatible with parameter of type "LPCWSTR" (and) E0513 a value of type "const char *" cannot be assigned to an entity of type "LPCWSTR"

2 – Types and Values, Swift is a type-safe language, which means the language helps you to be clear about the types of values your code can work with. If part of your code requires a​  a value of type future<int> can not be assigned to a variable of type int this is the function to insert new row Future<int> addContact(String contact_name, String contact_phone) async { Database c = await getConnection; Map<String, dynamic> contact = getMap(contact_name, contact_phone); return await c.insert("contacts", contact); }

Comments
  • (*hourtimes) you forgot to index it as hourtimes[i] in the memory allocation. You keep over-writing the previous pointer.
  • I just noticed that you mentioned you're using C++ rather than C. You should have tagged C++ instead, in that case, or you'll get a bunch of answers from us C people assuming you're using C.
  • If you're using C++, why is the question tagged C, and why are you using malloc instead of std::vector<std::string>?
  • I think this works in c, but I'm using c++ This is what the basis of your problem is. You are thinking of c solutions and trying to get them to work in c++ instead of using c++
  • The C++ (and C) Standard doesn't say anything about "Read Only Memory", and very rarl;y will string literals be stored in ROM, though they may be stored in memory that detects writes after a program has loaded. Both standards do say that attempting to modify a string literal is undefined behaviour.
  • Use strncpy() instead to avoid a buffer overflow when copying large strings.
  • @RemyLebeau If you are positive that the string will fit in your buffer, there is no point in wasting compute cycles with the "safe" version. In this case the buffer is 100 bytes, and the string being copied is... significantly less. Otherwise, if we are going to assume it could possibly not fit, we'd have to perform more checks to ensure safety than just using strncpy. Gotta check the last character to see if it is a NULL-terminated and then decide what course of action to take if it's not.