Reputation: 153
The function should simply read a matrix. Why does it freeze after I enter the first character?
#include "stdafx.h"
#include <iostream>
using namespace std;
void as(char **p,int n,int m)
{
int i, j;
for (i = 0; i < n; i++)
for (j = 0; j < m; j++)
{
cout << "p[" << i << "][" << j << "]=";
cin >> p[i][j];
}
}
int main()
{
char *a[100];
as(a, 3, 3);
return 0;
}
Upvotes: 3
Views: 404
Reputation: 3911
You have a great problem in your code. You are facing a UB in:
char *a[100]; // an array of 100 pointer to a character
// it is not initialized yet
To correct your code:
char *a[100];
// Allocating the array of 100 elements on the heap:
for(int i(0); i < 100; i++){
a[i] = new char[100]; // let's say your array is n = m
}
as(a, 3, 3);
for(int i = 0; i < 3; i++){
for(int j(0); j < 3; j++)
cout << a[i][j] << ", ";
cout << endl;
}
Last but not least Don't forget to free up memory when you are done with the dynamic array:
for(int i = 0; i < 100; i++)
delete[] a[i];
Upvotes: 0
Reputation: 67476
I have for you simple pseudo array on mallocs reallocs and pointers. Maybe it will be interesting for you:
typedef struct arr_str_t{
size_t rows, columns;
char **table;
}dynamicStringTable_t;
int CreateStringTable(dynamicStringTable_t **ptr, int rows, int columns)
{
int result = 0;
*ptr = (dynamicStringTable_t *)malloc(sizeof(dynamicStringTable_t));
if (ptr == NULL) return - 1;
(*ptr)->rows = rows;
(*ptr)->columns = columns;
(*ptr) -> table = (char *)malloc(rows * columns * sizeof(char *));
if (*ptr == NULL)
{
free(*ptr);
return -1;
}
for (int i = 0; i < rows * columns; i++) (*ptr)->table[i] = NULL;
return 0;
}
char *getString(dynamicStringTable_t *ptr, int x, int y)
{
char *result = (ptr == NULL || x >= ptr->columns || y >= ptr->rows || !x || !y) ? NULL : "";
if (result != NULL)
{
result = ptr->table[x + y * ptr->rows];
}
return result;
}
int putString(dynamicStringTable_t *ptr, int x, int y, const char *str)
{
int result = (ptr == NULL || x >= ptr->columns || y >= ptr->rows || str == NULL || !x || !y) * -1;
if (!result)
{
char *tmp = (char *)realloc(ptr->table[x + y * ptr->rows], (strlen(str) + 1) * sizeof(char));
if (tmp == NULL) result = -2;
else
{
ptr->table[x + y * ptr->rows] = tmp;
strcpy(tmp, str);
}
}
return result;
}
int removeString(dynamicStringTable_t *ptr, int x, int y)
{
int result = (ptr == NULL || x >= ptr->columns || y >= ptr->rows || !x || !y) * -1;
if (!result)
{
free(ptr->table[x + y * ptr->rows]);
ptr->table[x + y * ptr->rows] = NULL;
}
return result;
}
int destroyStringTable(dynamicStringTable_t *ptr, int x, int y)
{
int result = (ptr == NULL || x >= ptr->columns || y >= ptr->rows || !x || !y) * -1;
if (!result)
{
if (ptr->table != NULL)
{
for (int i = ptr->rows * ptr->columns - 1; i >= 0; i--)
free(ptr->table[i]);
free(ptr->table);
}
free(ptr);
}
return result;
}
Upvotes: 0
Reputation: 73376
This is undefined behavior: your array is an array of 100 pointers to char
. But you've never initialized them. So when you address p[i]
it gets an uninitialized pointer that could point anywhere, and when you dereference it with p[i][j] you might then freeze or suffer of anyother symptom of undefined behavior.
Solution 1: define your array as char a[100][100];
Solution 2: in the outer loop of as(), start to allocate the chars with p[i] = new char[m];
Solution 3: Forget about memory allocation and deallocation and use vectors instead. The vectors are totally dynamic, so no maximum of 100 rows anymore:
void as(vector<vector<char>> &p, int n, int m)
{
p.resize(n);
int i, j;
for (i = 0; i < n; i++) {
p[i].resize(m);
for (j = 0; j < m; j++)
{
cout << "p[" << i << "][" << j << "]=";
cin >> p[i][j];
}
}
}
int main()
{
vector<vector<char>>a;
as(a, 3, 3);
return 0;
}
If you want to try online...
Solution 4: you want modern C++, but you'd like to use your the elements in a[]
as they were a string, for easy output and manipulation, just use the same code as above but replace vector<vector<char>>
with vector<string>
And here you can look online the slightly simplified code.
Upvotes: 3