I'm actually trying, with a bi-dimensionnal array of string ( It could bi tri-dimensionnal in fact ), to exchange two "cells" of the array.
Before, i used memcpy but when comes long string, comes long execution time so I thought it was possible to simply exchange the pointers of the array but I don't know how to do :(
Here's my code:
#include <stdio.h>
#include <stdlib.h>
void fonction(unsigned char*** tab);
int main()
{
unsigned char*** tab;
tab = malloc(sizeof(unsigned char**) * 3);
if (tab == NULL)
exit(0);
for (int line = 0; line < 3; line++)
tab[line] = malloc(sizeof(unsigned char*) * 3);
for (int line = 0; line < 3; line++)
for (int column = 0; column < 3; column++)
tab[line][column] = malloc(sizeof(unsigned char) * 5);
for (int line = 0; line < 3; line++)
for (int column = 0; column < 3; column++)
for (int cell = 0;cell < 5;cell++)
tab[line][column][cell] = line * 3 * 5 + column * 5 + cell;
for (int i = 0;i < 32;i++)
fonction(tab);
for (int line = 0; line < 3; line++)
for (int column = 0; column < 3; column++)
free(tab[line][column]);
for (int line = 0; line < 3; line++)
free(tab[line]);
free(tab);
return 0;
}
void fonction(unsigned char*** tab)
{
unsigned char temp[5] = { 0, 0, 0, 0, 0 };
int alea = 0;
int alea2 = 0;
int alea3 = 0;
int alea4 = 0;
srand(58);
for (int line = 0; line < 3; line++)
{
for (int column = 0; column < 3; column++)
{
alea = rand() % 3;
alea2 = rand() % 3;
alea3 = rand() % 3;
alea4 = rand() % 3;
*temp = tab[alea][alea2];
tab[alea][alea2] = tab[alea3][alea4];
tab[alea3][alea4] = *temp;
}
}
}
Errors come when freeing tab and this is expectable because fonction does any old thing :)
Thanks in advance !
I am not sure why you are using
unsigned char temp[5] = { 0, 0, 0, 0, 0 };
Just use
char* temp;
And do swapping as
temp = tab[alea][alea2];
tab[alea][alea2] = tab[alea3][alea4];
tab[alea3][alea4] = temp;
I couldn't really get what you were trying to do with your code, but here's a working example of moving string pointers around:
#include <stdio.h>
#include <stdlib.h>
#define STR_NUM 3
int main(void)
{
char * str1[STR_NUM] = {"one", "two", "three"};
char * str2[STR_NUM];
int i;
for (i = 0; i < STR_NUM; ++i)
puts(str1[i]);
for (i = 0; i < STR_NUM; ++i)
str2[i] = str1[i];
for (i = 0; i < STR_NUM; ++i)
puts(str2[i]);
return 0;
}
Related
I have to dynamically allocate a 2d char array where the number of columns and lines are given by the user. After that, I need to create a copy of that 2d char array so I can manipulate its data.
This is how I tried it. When I try to print the copy array, it prints ok, but it gives me a Segmentation fault (core dumped) error. Not sure what I have to do here.
char **original;
original = malloc(sizeof(char *) * lines);
int i, j;
for (i = 0; i < columns; i++)
{
original[i] = malloc(sizeof(char) * columns+ 1);
}
for (i = 0; i < lines; i++)
{
for (j = 0; j < columns; j++)
{
scanf(" %c", &original[i][j]);
}
}
//The following happens inside a different function. The original matrix was passed as a parameter.
char **copy = NULL ;
for (i = 0; i < lines; i++)
{
copy[i] = malloc(sizeof(char) * columns+ 1);
}
for (i = 0; i < lines; i++)
{
strcpy(copy[i],original[i]);
}
The first for is not correct you should do it on lines not column.
You can also copy with memcpy. or copy char by char.
#include <stdio.h>
#include <stdlib.h>
int main(void) {
char **original;
int lines = 3;
int columns = 2;
original = malloc(sizeof(char *) * lines);
int i, j;
for (i = 0; i < lines; i++)
{
original[i] = malloc(sizeof(char) * columns);
}
for (i = 0; i < lines; i++)
{
for (j = 0; j < columns; j++)
{
scanf(" %c", &original[i][j]);
}
}
//The following happens inside a different function. The original matriz was passed as a parameter.
char **copy = malloc(sizeof(char *) * lines);
for (i = 0; i < lines; i++)
{
copy[i] = malloc(sizeof(char) * columns);
}
for (i = 0; i < lines; i++)
{
for (j = 0; j < columns; j++) {
copy[i][j] = original[i][j];
}
}
return 0;
}
The code below works fine when M <= 4, but seems to bugger up if you put in a whole number that's any bigger (in this case, I actually need M to be 10). Does anyone know why this is happening and what can be done about it? Thanks.
/*
"Read all 100 numbers from the text file and store it in a 10x10 array."
*/
#include <stdio.h>
#include <stdlib.h>
FILE *fptr;
int n;
int M = 4; // Length and width of array
int main()
{
// Allocating memory
int **myArray = (int **)malloc(M * sizeof(int));
for (int j = 0; j < M; j++) {
myArray[j] = (int *)malloc(M * sizeof(int));
}
// Loading data into array
fptr = fopen("List of Numbers.txt","r");
for (int i = 0; i < M; i++) {
for (int j = 0; j < M; j++) {
fscanf(fptr,"%d",&n);
// printf("%d ",n);
myArray[i][j] = n;
}
}
fclose(fptr);
// Printing
for (int i = 0; i < M; i++) {
for(int j = 0; j < M; j++) {
printf("%d ",myArray[i][j]);
}
printf("\n");
}
return 0;
}
This line:
int **myArray = (int **)malloc(M * sizeof(int));
should be:
int **myArray = (int **)malloc(M * sizeof(int *));
^
You are allocating an array of pointers, not an array of ints.
my program fails always in the 4th realloc and I don't understand why it makes the 3 first good and then it fails. I'm using triple pointer and I've tried to debugate and all works good until the 4th realloc when it says that its out of scope and segmention fault.
#include <stdio.h>
#include <stdlib.h>
void rellenar(int ***movimiento, int mov, int discos) {
int j, pos, mov2, ***movimiento2;
mov2 = mov + 1;
movimiento = realloc(movimiento, mov2 * sizeof (int **));
movimiento[mov] = malloc(3 * sizeof (int *)); //3 de torres
for (j = 0; j < 3; ++j) {
movimiento[mov][j] = malloc(discos);
}
printf("%d\n", mov);
for (pos = 0; pos < discos; pos++) {
movimiento[mov][0][pos] = 1;
movimiento[mov][1][pos] = 1;
movimiento[mov][2][pos] = 1;
printf("%d", movimiento[mov-1][0][pos]);
printf("%d", movimiento[mov-1][1][pos]);
printf("%d\n", movimiento[mov-1][2][pos]);
}
}
int main(int argc, char** argv) {
int ***movimiento, i, j, movs = 1, discos = 4, pos, mov;
movimiento = malloc(movs * sizeof (int **));
for (i = 0; i < movs; ++i) {
movimiento[i] = malloc(3 * sizeof (int *)); //3 de torres
for (j = 0; j < 3; ++j) //3 de torres
{
movimiento[i][j] = malloc(discos);
}
}
for (pos = 0; pos < discos; pos++) {
movimiento[0][0][pos] = discos - pos;
movimiento[0][1][pos] = 0;
movimiento[0][2][pos] = 2;
}
for (pos = 0; pos < discos; pos++) {
printf("%d", movimiento[0][0][pos]);
printf("%d", movimiento[0][1][pos]);
printf("%d\n", movimiento[0][2][pos]);
}
for (mov = 1; mov < 6; mov++) {
rellenar(movimiento, mov, discos);
}
}
What's wrong?
Rather than triple-nested mallocs and reallocs, which are inefficient and hard to keep straight, just do your allocation in a single shot:
size_t total = mov2 * 3 * discos;
int* movimiento = malloc(total * sizeof(int));
Now you can fill it like this:
for (size_t ii = 0; ii < total; ++ii)
movimiento[ii] = 1;
Then make an indexing function:
int* get_cell(int* matrix, size_t x, size_t y, size_t z, size_t max_y, size_t max_z) {
return &matrix[x*max_y*max_z + y*max_z + z];
}
So then:
int two = *get_cell(movimiento, mov, 1, pos, 3, discos);
And when it's time to release the memory, it's simply:
free(movimiento);
I would like to return a shape(trapezium) with bases 6 and 3 given integers 3 and 4 and a char.
I have tried implementing this with code below but I am getting a rectangle instead
#include <stdio.h>
char my_array[];
char *ptr;
int m = 3,n =4;
int main(void)
{
int i,j;
ptr = &my_array[0];
for (j = 0;j < n ;++j)
{
for (i = 0; i < m+n-1; i++)
{
my_array[i] = '*';
printf("%c ",my_array[i]);
}
printf("\n");
}
return 0;
}
I would like to know how I can reduce the length of each row of the result above to get the shape i need.Any ideas?
You probably meant to use i < m + j - 1 in your second for loop:
#include <stdio.h>
const int m = 3, n = 4;
int main(void){
int i, j;
const char symb = '*';
for (j = 0; j < n ;++j){
for (i = 0; i < m + j - 1; i++)
printf("%c ",symb);
printf("\n");
}
return 0;
}
I would like to fill 2d array, and this is what I do. However, it would give compile errors such as warning: value computed is not used and i dont understand why. I would appreciate if anyone could show me the problem and explain what could be done. thanks!
#include <stdio.h>
#include <string.h>
int main()
{
int array1[4][4];
int len = sizeof(array1) / sizeof(array1[0]);
int wid = sizeof(array1[0]) / sizeof(array1[0][0]);
int i, j , z = 0;
//compile error
for(i = 0, i < len; i++)
{
for(j = 0, j < wid; j++)
{
array1[i][j] = z;
}
z++;
}
int a, b;
for(a = 0, a < len; a++)
{
for(b = 0, b < wid; b++)
{
printf("%d", array1[a][b]);
}
}
return 0;
}
You have put a comma after the initialization part of each of your for statements. You should put a semicolon. For example, you wrote this:
for(i = 0, i < len; i++)
You need to change it to this:
for(i = 0; i < len; i++)
Also, you will probably want to print spaces between array elements, and a newline after each row:
for(a = 0; a < len; a++) {
for(b = 0; b < wid; b++) {
printf("%d ", array1[a][b]);
}
printf("\n");
}