[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y ] [Home]
4chanarchives logo
How to fill multidimensional array of chars in c++ when his size
Images are sometimes not shown due to bandwidth/network limitations. Refreshing the page usually helps.

You are currently reading a thread in /g/ - Technology

Thread replies: 24
Thread images: 2
File: 1046.jpg (52 KB, 471x694) Image search: [Google]
1046.jpg
52 KB, 471x694
How to fill multidimensional array of chars in c++ when his size is unknown yet?!
>>
>>51847348
Loop that shit up.
>>
If you don't know the size of the array you're doing something wrong.

Also C++ does not have true multidimensional arrays. it only has jagged arrays
>>
I tried to find it with Google. Many people suggest using vectors instead of arrays for that. I'm not able to test it atm. Good luck finding it out.
>>
Loop that shit my nigga
>>
>>51847348
Loop it up.
>>
>>51848197
>>51847368
i tried.
I just want to save inputs in multidimensional array. first i'm slicing temporary string into char array and then try to fill the multidimensional array with chars. But it only works when i know how many inputs the user will procide.
this is difficult as shit, specially when you have low c++ skills.
>>
>>51848332
I don't see how that would be a problem at all, as long as you allocate the array on the heap.
>>
>>51848425
this is effect of my 15 hours of programming and still works like shit.
#include <iostream>
#include <cstring>
using namespace std;

void test(int j=0)
{
string temp;
while (j!=4)
{
if (j==0)
cout << "text1: \n";
else if(j==1)
cout << "test2? \n";
else if(j==2)
cout << "text3\n";
else if(j==3)
cout << "text4 \n";
j++;
getline(cin, temp);
int lenght = temp.size();
char test[lenght+1];
strcpy(test, temp.c_str());
char tab[4][lenght];
for(int i=0; i!=lenght+1; i++)
{
tab[j][i]=test[i];
}
temp.clear();
}

}

int main()
{
int k=0;
test();
return 0;
}
>>
>>51848466
>lenght

DESIGNATED
>>
>>51848466
Wtf are trying to achieve? Where are the chars supposed to be in the multidimensional array?
>>
File: trap_programmer2.png (2 MB, 1200x1726) Image search: [Google]
trap_programmer2.png
2 MB, 1200x1726
>>51848466
Try /dpt/ and pray to git gud fag
>>
>>51847348
>>51848466
Wtf are you doing m8 just use std::vector
>>
>>51848614
>mfw this is gonna work
i love you anon
>>
>>51848466
You're not allocating it on the heap, though. Of course it doesn't work.
>>
>>51848693
what is the difference between heap and stack?
>>
>>51848912
Every function call creates a stack frame in memory, whose size is fixed (and must be known at compile time). This is where your variables live. The heap is a large, contiguous block of memory that your variables may point to. You can request an allocation on the heap at runtime, and so the size of your allocated data can depend on run-time parameters. You also use the heap to store things that are supposed to persist between function calls (stack frames are deallocated once the function returns).

That's the textbook answer. Go read up on it to see how to actually use it in practice, and learn some dos and don'ts. Be prepared to run into segmentation faults.
>>
>>51848466
guys don't reply to this bait, no one would spend 15 hours on something that could be done with

string s;
vector<string> v;
v.push_back(s);
>>
>>51849112
>ou can request an allocation on the heap at runtime
does it call - runPE?
>>
>>51848466
>int lenght
>>
>>51850155
what is runPE?
>>
>>51848466
>this is effect of my 15 hours of programming
this is why we shouldn't hire code monkeys
>>
>>51848466
>This is the competency of an average /g/entooman
>>
>>51847348
by not using C you faggot.

#include <array>

void foo() {
std::array<char*> arr;
}
Thread replies: 24
Thread images: 2

banner
banner
[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y] [Home]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
If a post contains personal/copyrighted/illegal content you can contact me at [email protected] with that post and thread number and it will be removed as soon as possible.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com, send takedown notices to them.
This is a 4chan archive - all of the content originated from them. If you need IP information for a Poster - you need to contact them. This website shows only archived content.