C $ C $Ç将无法完成运行 [英] C code won't finish running
问题描述
我很新的C,这是我想要做的工作测试程序。的目的是把从一个动态产生的矩阵的字符转换成另一种。在code我得到了编译,但从来没有运行完毕。
当我在底部注释掉循环会做的printf语句很好,但是当我取消它它只是不断运行,并且不打印。我既是C顺序的工作?如果在循环的东西坏了为什么它影响了printf语句?
下面是code:
的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&string.h中GT;
无效的主要(无效)
{
诠释N,M,I;
焦炭**矩阵=(字符**)的malloc(M *的sizeof(字符*));
对于(i = 0; I<米;我++)
{
矩阵[I] =(字符*)malloc的(N * sizeof的(炭));
}焦炭** oldMatrix =(字符**)的malloc(M *的sizeof(字符*));
对于(i = 0; I<米;我++)
{
oldMatrix [I] =(字符*)malloc的(N * sizeof的(炭));
}N = 1;
M = 2;
INT INDC;
矩阵[N] [M]。
矩阵[1] [1] ='1';
矩阵[1] [2] ='2';
oldMatrix [1] [2];
的printf(%C%C,矩阵[1] [1],矩阵[1] [2]);INT R = 0; 为(INDC = 0; INDC&下; = 1; INDC ++)
{
输出(4);
oldMatrix [R] [INDC] =矩阵[R] [INDC];
}}
在这里多的问题:
第一个问题:您同时使用 M
和 N
分配之前任何有效的值(因此它们的初始值是最有可能的巨大)。
问题二:您正在运行出界:
N = 1;
M = 2;矩阵[N] [M]。 //此行不会做任何事情
矩阵[1] [1] ='1';
矩阵[1] [2] ='2';
在C(和C ++)数组索引从0开始,所以数组的第一个元素是0,最后一个会是以下任一元素的数量(例如,使用 X <数组/ code>元素将主要从
阵去[0]
到数组[X-1]
)。
如果您的数组有一个元素(矩阵[N]
解析为矩阵[1]
)你'重新只能访问矩阵[0]
,矩阵[1]
将出界(即未定义的行为;不这样做!)。
第三个问题:你分配你的指针你换尺寸的方式:矩阵
将米
元素中存储将有 N
元素每个数组。您的其他code期望的正好相反。
I'm very new to C, this is a test program that i'm trying to make work. The purpose is to put the characters from one dynamically generated matrix into another. The code i've got compiles but never finishes running.
When I comment out the loop at the bottom it will do the printf statement fine, but when I uncomment it it just keeps running and doesn't print. I though C worked sequentially? If something in the loop is broken why is it affecting the printf statement?
Here is the code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void main (void)
{
int n,m,i;
char **matrix = (char**) malloc(m * sizeof(char*));
for ( i = 0; i < m; i++ )
{
matrix[i] = (char*) malloc(n * sizeof(char));
}
char **oldMatrix = (char**) malloc(m * sizeof(char*));
for ( i = 0; i < m; i++ )
{
oldMatrix[i] = (char*) malloc(n * sizeof(char));
}
n=1;
m=2;
int indc;
matrix[n][m];
matrix[1][1]='1';
matrix[1][2]='2';
oldMatrix[1][2];
printf("%c %c",matrix[1][1],matrix[1][2]);
int r=0;
for (indc=0; indc<=1; indc++)
{
printf("4");
oldMatrix[r][indc]=matrix[r][indc];
}
}
Multiple problems here:
First problem: You're using both m
and n
before assigning them any valid value (so their initial value is most likely huge).
Second problem: You're running out of bounds:
n=1;
m=2;
matrix[n][m]; // this line doesn't do anything
matrix[1][1]='1';
matrix[1][2]='2';
In C (and C++) array indexes start at 0, so the first element in an array would be 0, the last one would be one below the number of elements (e.g. an array with x
elements will essentially go from array[0]
to array[x-1]
).
If your array has one element (matrix[n]
which resolves to matrix[1]
) you're only able to access matrix[0]
, matrix[1]
will be out of bounds (i.e. undefined behavior; don't do it!).
Third problem: The way you're allocating your pointers you're swapping dimensions: matrix
will have m
elements and every array stored within will have n
elements. Your other code expects the exact opposite.
这篇关于C $ C $Ç将无法完成运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!