I have a problem when i'm printing char** array - c

I'm working with grap.
My graph is a structure like this
typedef struct{
int order; // number of node
int **mat; // matrix graphe
}graphe;
I'm working on school project and I need to build a set of binary's number from 0 to N (where is the order of the graph)
Actually I did this, it's working. When I'm printing the final variable, it displays all declinaison of binary number (000, 001, 010, 011, etc...)
char** construireSousEnsemble(graphe g){
int size = pow(2, g.order);
char** D = (char**)malloc(sizeof(char*)*g.order-1);
for (int i = 0; i < size; i++){
D[i] = (char*)malloc(g.order-1);
char buffer[g.order-1];
char tmp[g.order-1];
char final[g.order-1];
for (int j = g.order-1; j >= 0; j--){
int bin = (i >> j)&1;
sprintf(buffer, "%d", bin);
snprintf(tmp, sizeof(tmp), "%s", buffer);
strcat(final, tmp);
if (j == 0){
strcpy(D[i], final);
//printf("%s\n", D[i]);
//printf("%d | %s\n", i, D[i]);
memset(final, 0, sizeof(final)); // reset the zone
}
}
//printf("\n");
}
return D;
}
But in the main function, when I'm calling the function like this:
char** zones = construireSousEnsemble(g);
But when I'm printing the content with zones, I have this:
So I'm a bit lost.
This example is for a 3 nodes graph. If I have a 4 nodes, the weird symbol increase and I won't have 0001 or 0010 etc.., same with 5 or 6 nodes.
So my question is, why is this happening?
By the way, I'm not confortable with C so maybe I made some mistakes.
Thank you all :)

Here is the solution (posting if for the future)
char ** construireSousEnsemble(graphe g){
int size = pow(2, g.order);
char **D = (char **) malloc (sizeof (char *) * size);
char buffer[g.order];
char final[g.order];
for (int i = 0; i < size; i++) {
D[i] = (char *)malloc(g.order + 1);
final[0] = 0;
for (int j = g.order - 1; j >= 0; j--) {
int bin = (i >> j) & 1;
sprintf (buffer, "%d", bin);
strcat (final, buffer);
}
strcpy (D[i], final);
}
return D;
}

Related

Converting Static 2D Array to Dynamic Array in C

We were asked to convert 2D static array to dynamic array. So I will need to create an array of pointers in which every pointer points to a different row. I have written this code but my code breaks when i=1 on line *(dynamicStr[i] + v) = rowStr[v]; Additionally, if I enable free(ptr); section my debugger gets stuck there for 6 or 7 times and then contiunes.
EDIT: In the end, I solved the problem with appying the answers #dodooft and #Viktor Terziev gave.
#include <stdio.h>
#include <stdlib.h>
void toDynamic(int x,int y, char toDyna[x][y]);
void toDynamic2(int x,int y, char toDyna[x][y]);
int main()
{
char toDyna[7][12] = {
"JOHN",
"MARK",
"PIERCEPIERCE",
"20",
"ROSIE",
"ALEX",
"MARLYN"
};
int x = 7;
int y = 12;
toDynamic2(x, y, toDyna);
return 0;
}
void toDynamic2(int x,int y, char toDyna[x][y]){
char *dynamicStr[x];
int rowToCheck = 0;
int size;
char *ptr;
int c;
for(int i = 0; i < x; i++){
printf("i: %d\n",i);
c = 0;
size = strlen(toDyna[rowToCheck]);
ptr = (char*) malloc(size * sizeof(char));
for(int j = 0; j < y; j++){
if(toDyna[i][j] != '\0'){
*(ptr+c) = toDyna[i][j];
c++;
} else{
break;
}
}
*(ptr+size) = '\0';
printf(" ");
char rowStr[size];
for(int v = 0; v < size; v++){
rowStr[v] = *(ptr+v);
printf("Added Char: %c\n", rowStr[v]);
*(dynamicStr[i] + v) = rowStr[v];
}
//free(ptr);
//printf("\n%s\n", rowStr);
//dynamicStr[i] = &rowStr;
rowToCheck++;
}
for(int i = 0; i < x; i++){
printf("%s\n", dynamicStr[i]);
}
}
EDIT: This is the working verion of the code:
#include <stdio.h>
#include <stdlib.h>
char** toDynamic(int x,int y, char toDyna[x][y]);
void free2DArray(int x, char **dynamicStr);
int main()
{
char toDyna[7][12] = {
"JOHN",
"MARK",
"PIERCEPIERCE",
"20",
"ROSIE",
"ALEX",
"MARLYN"
};
int x = 7;
int y = 12;
char **dynamicArr;
dynamicArr = toDynamic(x, y, toDyna);
free2DArray(x, dynamicArr);
return 0;
}
char** toDynamic(int x,int y, char toDyna[x][y]){
printf("Q2\n");
char **dynamicStr;
int rowToCheck = 0;
int size;
int c;
dynamicStr = (char*)malloc(x * sizeof(char*));
for(int i = 0; i < x; i++){
dynamicStr[i] = (char*)malloc(y * sizeof(char));
c = 0;
size = strlen(toDyna[rowToCheck]);
char *ptr = (char*) malloc((size + 1) * sizeof(char));
for(int j = 0; j < y; j++){
if(toDyna[i][j] != '\0'){
*(ptr+c) = toDyna[i][j];
c++;
} else{
break;
}
}
*(ptr+size) = '\0';
dynamicStr[i] = ptr;
rowToCheck++;
}
for(int i = 0; i < x; i++){
printf("%s\n", dynamicStr[i]);
}
printf("----------------------------\n");
return dynamicStr;
}
void free2DArray(int x, char **dynamicStr){
printf("Q3\n");
for(int i = 0; i < x; i++){
free(dynamicStr[i]);
printf("dynamicStr %d freed\n", i);
}
free(dynamicStr);
printf("dynamicStr array freed\n");
printf("----------------------------\n");
}
You define dynamicStr as an array of char pointers, when you are trying to assign a value to it with *(dynamicStr[i] + v) = rowStr[v]; you are basically copying the value of rowStr[v] to the address that is pointed by dynamicStr[i] + v. That address is not defined in your code, so you got a segfault.
If you are trying to fill dynamicStr with pointers to new arrays with dynamic memory, you should try something like
dynamicStr[i] = ptr;
where ptr is the pointer returned by the malloc call to the i-th row. Also, as you are working with strings you can use strcpy to copy the data from the static array to the dynamic one.
Its much easier than you think, please refer to strcpy documentation and strlen documentation, and (if you use my code) don't forget to free your memory.
char * * toDynamic2(size_t n, size_t m, char strings[n][m])
{
char * * arr = malloc(n * sizeof(char*));
for(size_t i = 0; i < n; ++i)
{
size_t size = strlen(strings[i]);
arr[i] = malloc((size + 1) * sizeof(char));
strcpy(arr[i], strings[i]);
}
for(size_t i = 0; i < n; ++i)
{
printf("%s\n", arr[i]);
}
return arr;
}

Problem with 2D dynamic allocation of arrays and passing it as parameter

I have the following problem:
I am populating a 2D array whose number of rows is the number of files I am reading. Therefore the number of columns in each row corresponds to the bytes of data read from the file.
With this picture in mind I have the following program:
I have to return a byte array of data read from each file and the size of each file. This is what I have:
void fillArrays(unsigned char **array, size_t dataSize[], int *nFiles)
{
printf("Calling fillArrays\n");
int i, j, nrows;
nrows = 3;
*nFiles = nrows; // assuming no. of files to be read = 3
dataSize = (size_t *)malloc(nrows * sizeof(size_t));
array = (unsigned char**)malloc(nrows * sizeof(unsigned char *));
dataSize[0] = 4; // assuming file 1 contains 4 bytes
dataSize[1] = 3; // assuming file 2 contains 3 bytes
dataSize[2] = 1;// assuming file 3 contains 1 bytes
//populating file data into a 2D array. Here for test purpose assuming each file has data = 0x03!!
for(i = 0; i < nrows; i++)
{
array[i] = (unsigned char *) malloc(dataSize[i] * sizeof(unsigned char));
for(j = 0; j < dataSize[i]; j++){
printf("round %d %d\n", i,j);
array[i][j] = 0x03;
printf("array [%d][%d] = %02X\n ", i, j , array[i][j]);
}
//array[i][dataSize[i]]= '\0';
}
}
int main(int argc, char *argv[])
{
unsigned char **fileArray;
int noFiles = 0;
size_t *fileSize;
fillArrays(fileArray,fileSize, &noFiles);
printf("Returned no. of files = %d\n", noFiles);
printf("fileSize[0] = %lu\n", fileSize[0]);
printf("fileSize[1] = %lu\n", fileSize[1]);
printf("fileSize[2] = %lu\n", fileSize[2]);
int j = 0;
for (int i = 0; i < noFiles; i++){
printf("i = %d\n", i);
for (int j = 0; j < fileSize[i]; j++){
printf("Obtained data from file %d : (fileArray [%d][%d]) = %02X\n ", i, i, j , fileArray[i][j]);
//j++;
}
}
return 0;
}
the code is segfaulting in the 'j' loop in the main function. Could someone throw some light?
How can this be done better?
Thanks.
You are passing fileArray by value, so in fillArrays you are modyfing copy of this pointer to pointer, so after executing fillArrays function, fileArray in main function is not affected - it is still uninitialized, you need to pass this variable by reference (pass pointer to fileArray):
fillArrays(&fileArray,fileSize, &noFiles);
then in fillArrays you need to add these modifications:
void fillArrays(unsigned char ***array, size_t dataSize[], int *nFiles)
{
//...
*array = (unsigned char**)malloc(nrows * sizeof(unsigned char *));
//...
(*array)[i] = (unsigned char *) malloc(dataSize[i] * sizeof(unsigned char));
///...
(*array)[i][j] = 0x03;
}
your array and dataSize are local to fillArrays function and
allocating memory to them inside fillArrays will not affect the arrays inside main.
// Allocates the memory to local variables
dataSize = (size_t *)malloc(nrows * sizeof(size_t));
array = (unsigned char**)malloc(nrows * sizeof(unsigned char *));
Hence you need to use pointer to pointer to char* for fileArray and
pointer to size_t* for fileSize.
Your prototype will become as below.
void fillArrays(unsigned char ***array, size_t **dataSize, int *nFiles)
And you call the function as below.
fillArrays(&fileArray,&fileSize, &noFiles);
Sample code:
void fillArrays(unsigned char ***array, size_t **dataSize, int *nFiles)
{
printf("Calling fillArrays\n");
int i, j, nrows;
nrows = 3;
*nFiles = nrows; // assuming no. of files to be read = 3
*dataSize = (size_t *)malloc(nrows * sizeof(size_t));
*array = (unsigned char**)malloc(nrows * sizeof(unsigned char *));
(*dataSize)[0] = 4; // assuming file 1 contains 4 bytes
(*dataSize)[1] = 3; // assuming file 2 contains 3 bytes
(*dataSize)[2] = 1;// assuming file 3 contains 1 bytes
//populating file data into a 2D array. Here for test purpose assuming each file has data = 0x03!!
for(i = 0; i < nrows; i++)
{
(*array)[i] = (unsigned char *) malloc((*dataSize)[i] * sizeof(unsigned char));
for(j = 0; j < (*dataSize)[i]; j++){
printf("round %d %d\n", i,j);
(*array)[i][j] = 0x03;
printf("array [%d][%d] = %02X\n ", i, j , (*array)[i][j]);
}
//array[i][dataSize[i]]= '\0';
}
}
int main(int argc, char *argv[])
{
unsigned char **fileArray;
int noFiles = 0;
size_t *fileSize;
fillArrays(&fileArray,&fileSize, &noFiles);
printf("Returned no. of files = %d\n", noFiles);
printf("fileSize[0] = %lu\n", fileSize[0]);
printf("fileSize[1] = %lu\n", fileSize[1]);
printf("fileSize[2] = %lu\n", fileSize[2]);
int j = 0;
for (int i = 0; i < noFiles; i++){
printf("i = %d\n", i);
for (int j = 0; j < fileSize[i]; j++){
printf("Obtained data from file %d : (fileArray [%d][%d]) = %02X\n ", i, i, j , fileArray[i][j]);
//j++;
}
}
return 0;
}
Alternate approach:
I suggest you to not have pointer to pointer to char* as parameter instead you just return the filled array from fillArrays as below.
char** fillArrays(size_t **dataSize, int *nFiles)
{
printf("Calling fillArrays\n");
int i, j, nrows;
char **array = NULL;
nrows = 3;
*nFiles = nrows; // assuming no. of files to be read = 3
*dataSize = (size_t *)malloc(nrows * sizeof(size_t));
array = (unsigned char**)malloc(nrows * sizeof(unsigned char *));
(*dataSize)[0] = 4; // assuming file 1 contains 4 bytes
(*dataSize)[1] = 3; // assuming file 2 contains 3 bytes
(*dataSize)[2] = 1;// assuming file 3 contains 1 bytes
//populating file data into a 2D array. Here for test purpose assuming each file has data = 0x03!!
for(i = 0; i < nrows; i++)
{
array[i] = (unsigned char *) malloc((*dataSize)[i] * sizeof(unsigned char));
for(j = 0; j < (*dataSize)[i]; j++){
printf("round %d %d\n", i,j);
array[i][j] = 0x03;
printf("array [%d][%d] = %02X\n ", i, j , array[i][j]);
}
//array[i][dataSize[i]]= '\0';
}
return array;
}
and you call the function as below.
fileArray = fillArrays(&fileSize, &noFiles);

Insertion code using file stream

Here is the task i'm working on:
I am given a txt file containing the list of student names, id numbers, schools, majors, and test scores.
Read this contents and copy to the structure in C.
Sort this list using insertion sort.
Print sorted list on the screen.
I checked my coding by muting some parts, there is an error with my insertion sorting function.
I have no idea which part is incorrect. It all makes sense to me. I need help :( here is the code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 1000
#define BUF_SIZE 80
typedef struct {
char name[20];
char studentID[10];
char department[20];
char major[20];
int mid;
int final;
} student;
FILE *fp;
void operation(student t, student list[], int j);
void insertion_sort(student list[], int n);
void printing(student list[], int n);
int main(int argc, char *argv[])
{
char filename[20] = "studentlist.txt";
int n = 1; /* (number of students) + 1 */
student list[MAX];
char buffer[BUF_SIZE];
int i;
fp = fopen(filename, "r");
while (1) {
if (fgets(buffer, BUF_SIZE, fp) == NULL)
break;
strncpy(list[n].name, buffer, strlen(buffer) - 1);
fgets(buffer, BUF_SIZE, fp);
strncpy(list[n].studentID, buffer, strlen(buffer) - 1);
fgets(buffer, BUF_SIZE, fp);
strncpy(list[n].department, buffer, strlen(buffer) - 1);
fgets(buffer, BUF_SIZE, fp);
strncpy(list[n].major, buffer, strlen(buffer) - 1);
fgets(buffer, BUF_SIZE, fp);
list[n].mid = atoi(buffer);
fgets(buffer, BUF_SIZE, fp);
list[n].final = atoi(buffer);
n++;
}
fclose(fp);
insertion_sort(list, n);
printing(list, n);
return 0;
}
void insertion_sort(student list[], int n)
{
int i;
student temp;
for (i = 2; i < n; i++) {
temp = list[i];
operation(temp, list, i - 1);
}
}
void operation(student t, student list[], int j)
{
list[0] = t;
while (t.studentID < list[j].studentID) {
list[j + 1] = list[j];
j--;
}
list[j + 1] = t;
}
void printing(student list[], int n)
{
int i;
for (i = 1; i < n; i++) {
printf(" %s ", list[i].name);
printf(" %s ", list[i].studentID);
printf(" %s ", list[i].department);
printf(" %s ", list[i].major);
printf(" %6d ", list[i].mid);
printf(" %6d ", list[i].final);
putchar('\n');
}
}
Continuing from the comments and the first answer, you are making tracking index error between the split insertion_sort and operation functions. There is no need for two functions when one will work (and arguably be smaller). There is little benefit to be obtained from the split (except for increased confusion).
Putting a simple insertion sort together in one function that will fit your needs (and using pointers to sort as opposed to repeatedly using the function copy-constructor to accomplish the swaps), you could do something similar to the following:
typedef struct {
int id, g;
} student;
void insertion_sort (student **list, int nmemb)
{
for (int i = 0; i < nmemb; i++)
for (int j = i; j > 0 && list[j]->id < list[j-1]->id; j--)
{
student *tmp = list[j];
list[j] = list[j-1];
list[j-1] = tmp;
}
}
Putting together a short (and limited struct member example), you can do something like the following to sort the student data by ID (or id below)
#include <stdio.h>
typedef struct {
int id, g;
} student;
void insertion_sort (student **list, int nmemb)
{
for (int i = 0; i < nmemb; i++)
for (int j = i; j > 0 && list[j]->id < list[j-1]->id; j--)
{
student *tmp = list[j];
list[j] = list[j-1];
list[j-1] = tmp;
}
}
void printing(student *list[], int n)
{
int i;
for (i = 0; i < n; i++) {
printf(" %d ", list[i]->id);
printf(" %d \n", list[i]->g);
}
}
int main (void) {
student s[] = { {.id = 5, .g = 72}, /* minimal student test data */
{.id = 2, .g = 91},
{.id = 4, .g = 77},
{.id = 1, .g = 96},
{.id = 3, .g = 85}};
int n = sizeof s / sizeof *s;
student *l[n];
for (int i = 0; i < n; i++) /* initialize pointers in l */
l[i] = &s[i];
insertion_sort (l, n); /* sort pointers in l */
printing (l, n); /* output sorted pointers */
return 0;
}
Example Use/Output
Sorted by student.id
$ ./bin/inssort_so
1 96
2 91
3 85
4 77
5 72
If you did want to avoid using the additional level of indirection involved in passing an array of pointers (actually a pointer-to-pointer-to-type), you can make your original approach work as follows:
void insertion_sort (student *list, int nmemb)
{
for (int i = 0; i < nmemb; i++)
for (int j = i; j > 0 && list[j].id < list[j-1].id; j--)
{
student tmp = list[j];
list[j] = list[j-1];
list[j-1] = tmp;
}
}
void printing (student *list, int n)
{
for (int i = 0; i < n; i++)
printf (" %d %d \n", list[i].id, list[i].g);
}
int main (void) {
student s[] = { {.id = 5, .g = 72},
{.id = 2, .g = 91},
{.id = 4, .g = 77},
{.id = 1, .g = 96},
{.id = 3, .g = 85}};
int n = sizeof s / sizeof *s;
insertion_sort (s, n);
printing (s, n);
return 0;
}
As you go forward, also consider passing a compare callback function to your sort routines. That allows you to sort many different types of data using your same sort routine -- just by changing the compare function (as is done for the C-library qsort routine.

Append a character to a list n times

I want to create random data for testing. I want to fill an array with 100 strings of random length with the letter 'A'.
example:
array[0] = "AAAAA"
array[1] = "AAAAAAAA"
array[2] = "A"
...
char **create_string()
{
char **array = malloc(sizeof(**array));
srand((unsigned int)time(NULL));
int random = 0;
int i, j;
for(int i=0; i<100; i++)
{
random = rand() % 100;
for(j=0; j < random; j++)
{
array[i] = // some sort of string append that would be cheap.
}
}
}
I was looking at this C string append and they use strcat. Is there a better way to solve my problem? Since I will be running in a loop to create those random size strings.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
char **create_string(size_t n) {
char **array = malloc(sizeof(char*) * n);
int i, j;
for(i=0; i<100; i++)
{
size_t sz = rand() % 100;
array[i] = malloc(sz + 1);
for(j=0; j < sz; j++) {
array[i][j] = 'A';
}
array[i][sz] = 0;
}
return array;
}
int main() {
char **array;
size_t i;
srand((unsigned int)time(NULL));
array = create_string(100);
for (i = 0; i < 100; i++)
printf("%s\n", array[i]);
return 0;
}
Alternatively, you can create a template string and copy required number of characters into each random string:
char **create_string(size_t n) {
char template[101];
char **array = malloc(sizeof(char*) * n);
int i;
for (i = 0; i < 100; i++)
template[i] = 'A';
template[100] = 0;
for(i = 0; i < n; i++) {
size_t sz = rand() % 100;
array[i] = malloc(sz + 1);
strncpy(array[i], template, sz);
array[i][sz] = 0;
}
return array;
}
This will depend on the distribution of string lengths that you want. This is a uniform distribution of string lengths, from 0 to 200.
int n = rand() % 200 * sizeof(*array);
array[i] = malloc(n + 1);
memset(array[i], 'A', n);
array[i][n] = '\0';
But you could have a Gaussian distribution, a Poisson distribution, etc.
char **create_string()
{
char **array = malloc(sizeof(char *) * 100);
srand((unisgned int)time(NULL));
int i;
for (i = 0; i <100;i++)
{
int random = rand() % 100;
array[i] = malloc(random);
memset(array[i],'A',random-1);
array[random-1] = '\0';
}
return array;
}
Problem for you to fix: what happens if random is 0? Also the random numbers will not be equaly distributed. Only modulo by a power of 2 will achieve that.
Here's an approach that doesn't put an upper bound on any individual string, but does put an exact bound on the total length of all strings. It also only calls malloc twice.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TOT_SIZE (5000) /* adjust to taste */
#define TOT_STRS (100)
char **create_strings()
{
char **array = (char**) malloc(TOT_STRS * sizeof(char *));
char *str = (char*) malloc(TOT_SIZE);
int zeros = 1;
int i;
memset(str, 'A', TOT_SIZE - 1);
str[TOT_SIZE - 1] = 0;
while (zeros < TOT_STRS)
{
int pos = rand() % TOT_SIZE;
if (str[pos] != 0)
{
str[pos] = 0;
zeros++;
}
}
array[0] = str;
for (i = 1; i < TOT_STRS; i++)
{
array[i] = array[i - 1] + strlen(array[i - 1]) + 1;
}
return array;
}
And a short test program:
int main()
{
char **a = create_strings();
int i;
for (i = 0; i < TOT_STRS; i++)
{
printf("%3d: %s\n", i, a[i]);
}
return 0;
}
This code assumes that all the random strings need to be non-overlapping. If they can overlap in memory, you only need one string, and an array of pointer to different starting points in that one string.
You do not have to allocate the real 100 strings. Firstly you just declare a long enough array char long_array[100]. and then you use random = rand() % 100; get the random. Secondly you just pass the long_array and the random to your function. Then your problem is solved.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
char **create_string(const size_t array_size,
const size_t string_size,
const unsigned char chr)
{
srand((unsigned)time(NULL));
char ** array = malloc(array_size * sizeof (char *));
size_t t;
for (t = 0; t < array_size; ++t) {
array[t] = malloc(string_size * sizeof(char));
array[t][string_size] = '\0';
memset(array[t], chr, (rand() % string_size) + 1);
}
return array;
}
main() {
char ** randstring = create_string(10, 7, 'A');
int t = 0;
for (; t < 10; ++t)
printf("%s\n", randstring[t]);
return 0;
}
Possible output
AAAAAA
AAAAA
AAAAAA
AAA
AAAAA
AAAAAA
AAAAAA
AAAAAAA
AAAA
AAAA

How to create a variable sized array, pass a variable 2d array to a function and return another 2d array?

How do you create an 2d array with variable size, pass that variable array, and return a new 2d array of different dimensions? I've been working on this for hours and I can't find a solution. I managed to create an array of pixel values for a PGM image, but now I'm trying to "rotate" the array, but this is getting incredibly complex since my compiler won't let me declare a variable-sized object.Thank you so much to those who answer.
This is the statement that calls the function. Somebody told me to use malloc since you can't create an array with variable size.
char *SpunArray = malloc(image->x * image->y * sizeof(PGMPixel));
SpunArray = Rotate90Array(image->x, image->y, CreatedArray);
This is the function:
//char * Rotate90Array(int x, int y, char *array[x][y] )
char * Rotate90Array(int x, int y, char *array )
{
printf("\nLine 179");
// These have to be swapped because the image is being rotated
char *RotatedArray = malloc(x * y * sizeof(char));
printf("\nLine 182");
int u = x - 1;
int v = y - 1;
int i = 0;
int j = 0;
printf("\nLine 187");
char *ptr;
printf("\nLine 189");
for (i = 0; i < x; i++)
{
printf("\nLine 192");
*ptr = RotatedArray[i];
printf("\nLine 194");
for (j = 0; j < y; j++)
{
printf("\nLine 197");
// *ptr = *(array[u-j][i]);
*(ptr+((j*x)+(u-i))) = *(array+((i*y)+j));
printf("\nLine 200");
ptr++;
printf("\nLine 202");
}
}
printf("\nLine 205");
return RotatedArray;
}
I'm using the MingGW gcc, and windows 8 if that helps.
You have a memory leak. Why do you creating two arrays instead one?
Do like this:
char *SpunArray = malloc(image->x * image->y * sizeof(PGMPixel));
Rotate90Array(image->x, image->y, CreatedArray, SpunArray);
void Rotate90Array(int width, int height, char *array, char *RotatedArray)
{
int i = 0;
int j = 0;
for (i = 0; i < width; i++)
{
for (j = 0; j < height; j++)
{
// check this line on correct
RotatedArray[i * height + j] = array[j * width + width - i - 1];
}
}
}
I think this code can work as expect:
char * Rotate90Array(int x, int y, char *array )
{
printf("\nLine 179");
// These have to be swapped because the image is being rotated
char *RotatedArray = malloc(x * y * sizeof(char));
printf("\nLine 182");
int u = x - 1;
int v = y - 1;
int i = 0;
int j = 0;
printf("Line 187\n");
char *ptr;
printf("Line 189\n");
ptr = RotatedArray; //I add this line
for (i = 0; i < x; i++)
{
printf("Line 192\n");
// *ptr = RotatedArray[i]; //I delete this line
printf("Line 194\n");
for (j = 0; j < y; j++)
{
printf("Line 197\n");
// *ptr = *(array[u-j][i]);
*(ptr+((j*x)+(u-i))) = *(array+((i*y)+j));
printf("Line 200\n");
// ptr++; // I delete this line
printf("Line 202\n");
}
}
printf("Line 205\n");
return RotatedArray;
}

Resources