hi hi

sopuse to be tow pointers that the first pointer is a string and the second is an empty char that puts out the same string from the first pointer but only as much latters that the user is giving

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<iostream>
using namespace std;

int main(){
	int n;
	int i = 0;
	char *st[] = {"howudoing?"};
	char *ch[20];
	
	cout << "enter a number:" <<endl;
	cin >> n;
	
	while(n > 0){
		ch[i] = st[i];
		i++;
		n--;
	}
	cout << *ch;
}
1. Thread title is not informative.
2. You don't ask any questions.
3. Types of st and ch are wrong. A C-string is not an array of pointers.
4. You don't validate your input.
5. You don't terminate ch in the C-string way.
closed account (jvqpDjzh)
I don't know what you want to declare and do, but you are declaring an array of pointers to char (char*) in both cases, do you know that?
1
2
	char *st[] = {"howudoing?"};//array with 1 pointer to char
        char *ch[20];//array that can contain 20 pointers to char 


EDIT: if you want to declare a const char pointer (C-string), just do this:
const char* str1 = "Hello Dudes!";
if you want to copy it to an array of chars, you can use strlen and strcpy:
1
2
3
4
5
6
int str1_len = strlen(str1);
char copy[str1_len + 1];//+ 1 for the '\0'
strcpy(copy, str1);

std::cout << "str1 = "<<str1<<'\n';
std::cout << "copy = "<<copy<<'\n';

Remember to include the header <cstring> for strlen and strcpy
Last edited on
@zwilu line 2 is invalid
You cannot have non-costexpr array dimensions in standard C++

OPs code accidentally works, but not the way he wants it.
To make it work how he wants it, he should:
1) Remove asteriscs in lines 7, 8 and 18
2) check if n is less than 20
3) add ch[i] = '\0' after loop but before output
closed account (jvqpDjzh)
You cannot have non-costexpr array dimensions in standard C++
So tell me why const char* is constant????? And why it's working?
const char* has a compile time defined size, I add to it 1 and the result is of course a constant.
Last edited on
const char* is not an array. it is a mutable pointer to constant data.

Arrays have own type of, say, int[10] and in C++ all type information should be known at compile time. Some compilers allows VLA as extentions but this is not a standard behavior and will not work for all.

So
1
2
3
4
5
6
7
int x[10]; // Valid
const int a = 5;
int y[a]; // valid
int b = calculate_size();
int z[b]; // invalid
const int c = get_dim();
int o[c]; //valid only if get_dim() is constexpr 
Last edited on
closed account (jvqpDjzh)
But this should work according to your reasoning: const char* const str1 = "Hello Dudes!";
1) "Hello Dudes!" Is a string literal usually stored in read only memory
2) const char* const str1 is a pointer (not an array) pointing to said literal. Data is marked const to avoid access violations, and pointer itself marked const so nobody will point it to another place.

It will work. It is standard. And it has no relation to issue with stack-allocated arrays requiring compile-time value as their dimension.
closed account (jvqpDjzh)
And it has no relation to issue with stack-allocated arrays requiring compile-time value as their dimension.
Maybe I am misunderstanding what you are saying here: your basically saying that I can use the length of a const char* const to determine the size of an array or the opposite?

if the opposite:
const char* const str1I know what this is. And once you assign to it a value, it won't change, but actually I don't understand why the compiler is not so smart enough to accept it as a constant.
Because you are using strlen function. It is runtime function. Not constexpr.
Because compiler have no right to make assumptions that data here will not really change: other pointer can try to change it, it can be placed in shared memory, etc.
Because that is the way C was designed very long ago. Because C++ tries to be compatible both ways and do not change parts of language inherited from C.

Becaue it is in the standard and all complaints should be sent to the C++ Standard Committee.
closed account (jvqpDjzh)
Ok, boss, thanks again, for me it's ok!
char* is a pointer. All pointers are about the same size, const or not.
"Hello Dudes!" is a constant string literal.

Now, you had a pointer to a constant string literal and you call strlen() with that pointer. The strlen will operate with the string, not with the pointer.

The compiler has two options:
1. Inject code that calls strlen() during runtime.
2. Somehow, miraculously, figure out that the function call can actually be evaluated during compilation, so that the compiled code is actually int str_len1 = 12;

However, your str_len1 is not a constant.
Topic archived. No new replies allowed.