i want to make array of pointer to char and assign it char
however array size is from user
for example:
user said 3
a[0]="xxxx"
a[1]="sssss"
a[2]="hhhh"
size of array is from user and size of string is also according to user
is this correct way to do this?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <stdlib.h>
#include <stdio.h>
usingnamespace std;
int main()
{ int k=0;
scanf("%d",&k);
char * l[k]={newchar[401]}; // how can i initialize every pointer of char?
char arrays[401];
char *s=arrays;
for(int i=0;i<k;i++){
scanf("%s",s);
strcpy(l[i],s); // why every time i copy and change s using scanf, the value also change. is there another way?
}
char * l[k] // this is an array of pointers. C++ does not have VLA.
=
{ newchar[401] }; // you make first pointer point to an array,
// and set the rest to 0 (hopefully nullptr)
char arrays[401];
char *s=arrays; // you set pointer s to paint to arrays
// you don't need s, you could use arrays directly
for(int i=0;i<k;i++){
scanf("%s",s); // you read into arrays
strcpy(l[i],s); // all put first iteration calls strcpy( 0, s );
}
C++ does not support VLA (but some compilers do as extension).
The "array" l should be dynamic.
For example, char* * l = newchar* [k];
What does happen when strcpy copies to invalid address?
To memory that has not been allocated for array of char? Undefined behaviour.
The new is C++. The scanf and strcpy are in C library. Usable in C++ code, but the C++ Standard Library has more convenient and much safer alternatives.
This is not exactly same as yours:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <string>
#include <vector>
#include <iostream>
int main()
{
size_t k {};
if ( std::cin >> k ) {
std::vector<std::string> foo;
foo.reserve( k );
std::string s;
int i = 0;
while ( i < k && std::cin >> s ) {
foo.emplace_back( s );
++i;
}
}
}
@Thomas: your line 12 is too verbose and line 9 has a magic number. (Lines 33-34 have typo.)
Lines 16-18 contain a design decision that every "row" has cols characters.
If one just needs to store the input, then one can:
1. read to buffer
2. malloc just enough for the row
3. copy from buffer into row
words holds an array of char*, so the allocation on line 12 should be:
char** words = malloc(rows * sizeof(char*));
The allocation of each word is incorrect. You need to allocate space for the string and the trailing null, then copy the string into that buffer. So the code should be:
1 2 3 4 5
for (int i = 0; i < rows; ++i) {
int len = snprintf(buffer, sizeof(buffer), "Line %d", i + 1);
words[i] = malloc(len + 1);
strncpy(words[i], buffer, len + 1);
}
There's a single standard library function that performs the last two steps:
1 2 3 4
for (int i = 0; i < rows; ++i) {
snprintf(buffer, sizeof(buffer), "Line %d", i + 1);
words[i] = strdup(buffer);
}
> size of array is from user and size of string is also according to user
This is a somewhat hard problem for a programmer just starting out with C. The difficult part is that a line of text entered by the user may be of an arbitrary length; we need to read character by character, resizing the buffer as required, till all characters in the line are placed into the buffer.