# Two dimensional char array

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 01-18-2006
eurus
Two dimensional char array
Here is what I basically need:

I need to be able to take in say 2 words on 10 lines
each word can be a maximum of 30 chars

heres what I have but it seems incorrect:

Code:

```char **c2names; c2names = (char **) malloc(amount_of_lines * sizeof(char *));         for(i = 0; i < amount_of_lines; i++)         {                 c2names[i] =(char *) malloc(30 * sizeof(char));         }```
• 01-18-2006
bithub
• 01-18-2006
Dave_Sinkula
• 01-18-2006
eurus
correct way to call it would be
Code:

`scanf("%s %s", c2names[0][0], c2names[0][0]);`
?
• 01-18-2006
quzah
Correct way to do what? To scan a string into a single "line", you'd do:
Code:

`scanf( "%s", c2names[ line ] );`
Assuming you actually wanted to use scanf...

Quzah.
• 01-18-2006
Dave_Sinkula
• 01-18-2006
eurus
yes but what if I want to take in 2 words
• 01-18-2006
quzah
Can't you think for yourself at all? I don't see your attempt to do anything here. Where is it? While you're posting it, how about posting what you think you're doing, so we actually know what it is you're trying? Give us a line by line of what you think is happening, so we can tell you if you're right or not.

Quzah.
• 01-18-2006
eurus
Well heres what is going on in my head,

I want to take in say 10 lines and 2 words per line (seperate)

so
Code:

```char **c2names; c2names = (char **) malloc(amount_of_lines * sizeof(char *)); for(i = 0; i < amount_of_lines; i++) {         c2names[i] =(char *) malloc(30 * sizeof(char)); } int i; for(i = 0; i < amount_of_lines; i++) { fscanf(fptr, "%s %s", c2names[i]); }```
sorry for even posting this question (I program mainly java NEVER C) =P
• 01-18-2006
quzah
scanf isn't really very good for reading strings. I mean, you can make it work with some bracket trickery, but you'd be much better off just looking at the link Dave provided above, and using fgets.

Quzah.
• 01-18-2006
Dave_Sinkula
Quote:

Originally Posted by eurus
Well heres what is going on in my head,

Why not post the actual input and expected output rather than a perceived solution to an undescribed problem? The scanf family is not for the inexperienced.
• 01-18-2006
eurus
ok

input via file
**
2 2
Hi there
my name
**

2 and 2 is the numbers that signify 2 rows. 2 columns

Code:

```char **c2names; int amount_of_lines, words; fscanf("%d %d", &amount_of_lines, &words); c2names = (char **) malloc(amount_of_lines * sizeof(char *)); for(i = 0; i < amount_of_lines; i++) {         c2names[i] =(char *) malloc(30 * sizeof(char)); } int i; for(i = 0; i < amount_of_lines; i++) { fscanf(fptr, "%s %s", c2names[i]); }```

is no output it should just store the hi there in one row of array and my name in second row where each word gets its own element

making each word accessable through array calling.

Now the question is do I use the int words in any of the malloc and am If I could access each element in fscanf am I doing it correctly.
• 01-18-2006
quzah
If you simply take the time to look at your code and think about what you're doing, you can see it's wrong.
Code:

`fscanf(fptr, "%s %s", c2names[i]);`
You have two scan codes in your fscanf statement, but you only provide 1 argument. Or...
Code:

`scanf("%s %s", c2names[0][0], c2names[0][0]);`
You actually do provide two arguments, but they're to the exact same spot. Thus, one will overwrite the other. (Well, if it wasn't incorrectly done it would have.)

Try compiling your code, rather than just posting in something random. Compile with warnings enabled.

Quzah.
• 01-18-2006
eurus
with the [0][0] though I get an error, only works with
Code:

`scanf("%s %s", c2names[0]);`
and that was a typo for the post should be
Code:

`scanf("%s %s", c2names[0][0], c2names[0][1]);`
I compile on a sep computer sorry =P
• 01-18-2006
quzah
Do you know why it doesn't work with [0][0]? If you don't understand, take the time to. It will help you out in the long run. Don't just try to kludge code together to get the "correct" output. Try to actually understand why it does or doesn't do what it does.

[0][0] doesn't work because that is the location of an actual single character in your "array". You're trying to read a string, which is a series of characters. Thus, you don't specify a single character to read it to. (It was wrong anyway, because were you trying to read into a single character, you would have needed to provied the address of the single character. Not the character itself.)

It works when you just provide [0], because that's referring to one line in your two dimensional array. It works out like so:
Code:

```char **twodee; twodee = malloc( numoflines * sizeof( char * ) ); /* allocate a bunch of lines. */ for( thisline = 0; thisline < numoflines; thisline++ )     twodee[ thisline ] = malloc( lengthofline );```
Now, just like you allocate each line, you read into the single line the same way. If you want to read into a character (one at a time) in a specific line, then you provide the line and character you want to read into:
Code:

`twodee[ line ][ character ] = 'a';`
Like so.

Quzah.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last