Does this make sense?

Does this make sense?

Is it the best way to dynamically allocate a 2d array and populate it?

This is C, if that isn't clear...

Yes.

sizeof(char) is 1 by definition

have you tried using logarithms?

Dear lord, no. Unless you have to, don't do this.

int main(int,char**)
{
int i,j,c,r;
char *arr;
c=8; r=8;
arr=malloc(c*r*sizeof(char)); /*c auto casts void* to anything. c++ does not*/
for( i=0;i

It's best to use literals as little as possible

Instead of using malloc, why don't you just say char arr[8][8]? While malloc isn't wrong, you are forgetting to free your memory afterwards. In this case since that's just your main method and the program will finish execution, you wouldn't have a problem. But if this was some other method which might be called repeatedly, you would have a memory leak.

Do people even still use c?

You make me want to break out my 20 year old textbook.

I don't know how. I'm very new to programming

I'm supposed to make a chess board like this. And it's supposed to be dynamically allocated.

Not sure what the best way to do it is...

it's supposed be dynamically allocated.

we're using a separate function to free the memory after the game is played through.

Maybe back when each bit was a handful of vacuum tubes you might have got 7 or even 6 bit chars to store your fucked up version of ascii.

who knows...

from what I hear, most of the shit they teach us is out of date.

You don't need your for loop that does the malloc at all. All you need to do is say this:

arr = (char **)malloc(r * c * sizeof(char));

Why is the array instantiated outside of the nested for?

I don't even know, man...

Why the fuck would you instantiate the array inside the nested for?

I meant assign the array a value. It don't matter

you should just
char arr[8][8];

That first allocation shouldn't be sizeof(char).

It should be sizeof(char*) because it is a pointer to char (array) not char itself.

>arr[i][0] = 'P';
>for(j=1;j

Ah, that makes sense now.

I'll save you some time user, this is a classic teaching example.

Calculate the determinant of the Hessian matrix (Hess) in C, which is designed to do this really fast.

C-Hess. CHESS.

Solve it that way and give your lecturer the smug knowing look - then you'll be bros.

Yeah, that's fucking hilarious. Is this just a troll thread?

Well the actual assignment is to make a program that reads something written in algebraic chess notation, and then print out what the chessboard looks like at that point of the game.

>I don't even get the joke
this thread is 100% sincere

I'm a fucking travesty

Oh. Now I feel bad. Well, don't worry, everyone has to start from the beginning. First, tell me more about the assignment. Do the characters have to be in a 2d array or do you just have to dynamically allocate memory and print a chessboard correctly?


◄►

The first malloc is an array of pointer to characters, not characters. It should be "r*sizeof(char *)". The first for loop is instantiating each row, not each column. It should increment to r, not c. What is going on inside that 2nd loop? Why did you pull out the first execution of the nested loop? What is the goal of the print statement, why is it mislabeling the output?

Its not vitally important as you are reaching the end of the main function and all program memory will be released, but it is good practice to ensure that all your mallocs are matched with calls to free on the pointers you have malloc'd

For this specific function, these are the guidelines we were given.

Yes, this. A 2d array in C has two parts:

1. The array, which is a list of pointers to strings (char*)

2. The rows, which are stings (char*) of the desired length

oops... ignore the print statement, I was jumping around without changing the label while I was seeing if the array was completely populated.

I figured the starting from 0 in the nested loop wasn't necessary since it's already covered in the first loop. Is that wrong?

I fixed the sizeof(char *) already, thanks.

Based on this guidance you have missed out a step which is good practice as well as specifically being asked for.

You need to perform a check on the pointer being returned from malloc to see if it is NULL as the malloc has failed to assign any memory (rare in todays big memory systems, but it does happen)

i.e.

if ( arr == NULL ) return 1;

right after the call to malloc

Create an array of r pointers to the rows

arr = (char **)malloc(r*sizeof(char *));

Create the individual rows of length c

for (i=0; i

C is zero indexed, you should have started from 0 and ended at c-1, not started from 1 to go to c

What first loop? arr[i][0] hadn't been set prior to line 21 and just starting the nested loop with "j=0" will cover that.

Oh, also note you're supposed to be writing this within a function that can be called. The code needs to go within a function char **createChessBoard(void) that you define outside of int main. That way someone can use your function to create a new chessboard using mychessboard = createChessBoard();

Thanks, everyone, you guys are real pals.

I'm sorry in advance if you see another thread in the next few days asking for help. This isn't even the hard function. The one that translates algebraic chess notation is giving everyone else in my class trouble, and I doubt I'll do much better.