什么是正确的初始化方法char**?我在尝试时遇到覆盖错误 - 未初始化指针读取(UNINIT):
char **values = NULL;
Run Code Online (Sandbox Code Playgroud)
要么
char **values = { NULL };
Run Code Online (Sandbox Code Playgroud) 我正在从一本书中自学C,我正在尝试创建一个填字游戏.我需要创建一个字符串数组但仍然遇到问题.另外,我对阵列不太了解......
这是代码片段:
char word1 [6] ="fluffy", word2[5]="small",word3[5]="bunny";
char words_array[3]; /*This is my array*/
char *first_slot = &words_array[0]; /*I've made a pointer to the first slot of words*/
words_array[0]=word1; /*(line 20)Trying to put the word 'fluffy' into the fist slot of the array*/
Run Code Online (Sandbox Code Playgroud)
但我不断收到消息:
crossword.c:20:16: warning: assignment makes integer from pointer without a cast [enabled by default]
Run Code Online (Sandbox Code Playgroud)
不确定是什么问题...我试图查找如何制作一个字符串数组但没有运气
任何帮助都感激不尽,
山姆
我最近开始编程C只是为了好玩.我在一个非常熟练的程序员C# .NET和Java台式机领域内,但这是谈到了是有点太多对我来说是挑战.
我试图做一些"简单"的事情,如从函数返回一个二维数组.我已经尝试过在网上进行研究,但我很难找到有用的东西.
这是我到目前为止所拥有的.它没有完全返回数组,它只填充一个.但即使这样也无法编译(我相信如果你是一名熟练的C程序员,原因必须是显而易见的).
void new_array (int x[n][n]) {
int i,o;
for (i=0; i<n; i++) {
for (o=0; o<n; o++) {
x[i][o]=(rand() % n)-n/2;
}
}
return x;
}
Run Code Online (Sandbox Code Playgroud)
用法:
int x[n][n];
new_array(x);
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?应该提到的n是具有该值的常量3.
编辑:尝试定义常量时,这是一个编译器错误:http://i.imgur.com/sa4JkXs.png
我试图在C中创建一个固定长度的"字符串"数组,但一直有点麻烦.我遇到的问题是我遇到了分段错误.
这是我的程序的目标:我想使用从文本文件读取的数据按索引设置数组的字符串.这是我当前代码的要点(我道歉,我无法添加我的整个代码,但它非常冗长,可能只会引起混淆):
//"n" is set at run time, and 256 is the length I would like the individual strings to be
char (*stringArray[n])[256];
char currentString[256];
//"inputFile" is a pointer to a FILE object (a .txt file)
fread(¤tString, 256, 1, inputFile);
//I would like to set the string at index 0 to the data that was just read in from the inputFile
strcpy(stringArray[i], ¤tString);
Run Code Online (Sandbox Code Playgroud) 我有一个给定大小的数组,没有使用任何内存分配,如何在其中添加内容?
假设我运行代码,它等待你要输入的内容,你输入"bond",我如何将它附加到数组中?A [10]?
像往常一样,我在这里阅读了很多帖子。我发现了一篇关于一般总线错误的特别有用的帖子,请参阅此处。我的问题是我无法理解为什么我的特定代码会给我一个错误。
我的代码是自学C语言的一次尝试,是对我学Java时做的一个游戏的修改。我的游戏的目标是获取一个巨大的 5049 x 1 文本文件。随机选择一个单词,将其打乱并尝试猜测它。我知道如何做到这一切。所以无论如何,文本文件的每一行都包含一个单词,例如:
5049
must
lean
better
program
now
...
Run Code Online (Sandbox Code Playgroud)
所以,我在C中创建了一个字符串数组,尝试读取这个字符串数组并将其放入C中。我没有做任何其他事情。一旦我将文件放入 C 中,其余的事情就很容易了。更奇怪的是它遵守了。当我用./blah命令运行它时,我的问题出现了。
我得到的错误很简单。它说:
zsh: bus error ./blah
Run Code Online (Sandbox Code Playgroud)
我的代码如下。我怀疑这可能与内存或缓冲区溢出有关,但这完全不科学,而且是一种直觉。所以我的问题很简单,为什么这个 C 代码会给我这个总线错误消息?
#include<stdio.h>
#include<stdlib.h>
//Preprocessed Functions
void jumblegame();
void readFile(char* [], int);
int main(int argc, char* argv[])
{
jumblegame();
}
void jumblegame()
{
//Load File
int x = 5049; //Rows
int y = 256; //Colums
char* words[x];
readFile(words,x);
//Define score variables
int totalScore = 0;
int currentScore = 0;
//Repeatedly pick a random work, …Run Code Online (Sandbox Code Playgroud) 我将如何获得下面字符串的长度以及数组大小?
字符 str [] = {};
Run Code Online (Sandbox Code Playgroud)str[0] = "He"; str[1] = "llo"; str[2] = " Wor"; str[3] ="ld";
我如何将它们存储在多维数组中?所以这个数组看起来像这样:
char strstr [str的大小][str的字符串长度];
所述 strstr[][]阵列的尺寸应
的数组元素的1.number STR和
在所有字符的2.number STR。在 的情况下 strstr[3][1],这将是“ d ”。
如果不首先初始化str数组,这可能吗?
[编辑] 我明白了,的第二维strstr没有意义。它应该是
[/edit]中每个元素的长度,*str而不是完整的字符数*str
这是我的问题:我在字符串矩阵中保存了一些相对路径.根据用户选择,我必须打开某个文件.问题是,当我使用fopen函数时,文件指针不指向任何东西.以下是代码示例:
#include <stdio.h>
#include <stdlib.h>
#define MAX_PATH 100
///Global matrix of strings, containing the paths used in fopen() function
char paths[MAX_PATH][3] = {{"c:\\Users\\ThisPc\\Desktop\\file1.txt"},
{"c:\\Users\\ThisPc\\Desktop\\file2.txt"},
{"c:\\Users\\ThisPc\\Desktop\\file3.txt"}};
int main(){
///Declaring and initializing the 3 file pointers to NULL
FILE *filepntr1 = NULL;
FILE *filepntr2 = NULL;
FILE *filepntr3 = NULL;
///Opening the 3 files with the correct arrays
filepntr1 = fopen(paths[1], "w");
filepntr2 = fopen(paths[2], "w");
filepntr3 = fopen(paths[3], "w");
///Typing something on the files opened, just to check if the files …Run Code Online (Sandbox Code Playgroud) 我知道如何使用二维数组制作字符串数组,但我可以这样做:
char array[];
array[1]="bob";
array[2]="cat";
Run Code Online (Sandbox Code Playgroud) 帮助我摆脱这个问题.我在ubuntu12.04上使用GCC.当我编写这个程序从键盘n获取5个字符串然后在屏幕上打印这些字符串.程序已编译但在执行期间它从键盘获取字符串但仅打印最后一个字符串.我写的程序如下:
void main()
{
char names[10];
int i,j;
for(i=0;i<5;i++)
{
printf(" Enter a name which you want to register\n");
scanf("%s",names);
}
for(i=0;i<5;i++)
printf(" the names you enter are %s\n", names);
}
Run Code Online (Sandbox Code Playgroud)