每当我运行程序"Assignment在没有强制转换的情况下从Integer发出指针"时,我都会收到此错误.我的代码写在下面....请帮助......谢谢
struct student {
char studentID[6];
char name[31];
char course [6];
};
struct student *array[MAX];
struct student dummy;
int recordCtr=0;
int read(){
FILE *stream = NULL;
int ctr;
char linebuffer[45];
char delims[]=", ";
char *number[3];
char *token = NULL;
stream = fopen("student.txt", "rt");
if (stream == NULL) stream = fopen("student.txt", "wt");
else {
printf("\nReading the student list directory. Wait a moment please...");
while(!feof(stream)){
array[recordCtr]=(struct student*)malloc(sizeof(struct student));
while(!feof(stream)) {
fgets(linebuffer, 46, stream);
token = strtok(linebuffer, delims); //This is where the error …Run Code Online (Sandbox Code Playgroud) 好吧,我一直在这里,我确切地知道故障的位置,但不知道如何解决它.我已经知道fgets和scanf对于这个程序会更好,但我做不到.
该程序大约10分钟前工作,然后我改变了它并得到了一个段故障.然后我把它改回去了,仍然有一个段错误.无论如何,我确信新鲜的眼睛会马上看到它.有它:D
PS:请注意我的(lessthan)而不是<因为我不知道如何在我的代码示例中正确地留下那些:(
#define WORDLENGTH 15
#define MAXLINE 1000
int main()
{
char *line[MAXLINE];
int i = 0;
int j;
int n;
char c;
for (n=0; c!=EOF; n++){
char *tmp = (char *) malloc(sizeof(char)*WORDLENGTH);
while ((c=getchar())!=' ')
tmp[i++]=c;
line[n]=tmp;
i=0;
printf("\n%s\n",line[n]); //
}
for(j = 0; j < n; j++){
printf("\n%s\n", line[j]);
free (line[j]);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我有以下代码:
void funcA(void* pArg)
{
STRUCTA abc;
.
. // Some processing here
.
if (pArg)
(STRUCTA *)pArg = abc;
}
Run Code Online (Sandbox Code Playgroud)
问题是,这段代码抛出了以下警告:警告:赋值目标不是真正的左值; 这将是一个艰难的错误
如果没有演员阵容,我会再次发出警告,我正试图取消引用无效指针......
由于警告被视为错误,我无法使用此代码 - 但我真的不能使用任何其他指针类型而不是void*作为参数.我缺少一个优雅的解决方案吗?
有没有办法让这项工作?
我对malloc有一个奇怪的问题.我有这个typedef:
typedef struct buffer {
int D;
int T;
unsigned int current_size;
unsigned int max_size;
pthread_mutex_t mutex;
pthread_cond_t non_pieno;
pthread_cond_t non_vuoto;
msg_t** messages;
struct buffer* (*buffer_init)(unsigned int);
void (*buffer_destroy)(struct buffer*);
} buffer_t;
Run Code Online (Sandbox Code Playgroud)
这是buffer_init和buffer_destroy函数:
buffer_t* buffer_init(unsigned int maxsize)
{
buffer_t* new_buffer = (buffer_t*)malloc(sizeof(buffer_t));
msg_t** new_messages = (msg_t**)calloc(maxsize, sizeof(msg_t**));
pthread_mutex_t mx = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t np = PTHREAD_COND_INITIALIZER;
pthread_cond_t nv = PTHREAD_COND_INITIALIZER;
new_buffer->T = 0;
new_buffer->D = 0;
new_buffer->current_size = 0;
new_buffer->max_size = maxsize;
new_buffer->messages = new_messages;
new_buffer->mutex = mx;
new_buffer->non_pieno = np; …Run Code Online (Sandbox Code Playgroud) 我正在用C开发一个链表.我从txt文件中获取数据.但是当我尝试运行程序时,它给了我一个getc()的分段错误.这是代码,
#include<stdio.h>
#include<stdlib.h>
struct node{
char surname[100];
int number[1o0];
char name[100];
struct node *next;
};
typedef struct node Node;
int main()
{
FILE *ifp;
Node first ,*current;
//current =&first;
int i=0,j=0;
int ch;
char num[100],name[100];
ifp = fopen("tele.txt","r");
while (ch != EOF)
{
while(i<4)
{
ch = getc(ifp);
num[i++] = ch;
}
num[i] = '\0';
i=0;
while(ch !='\n')
{
ch = getc(ifp);
if(ch != '\t')
name[j++]=ch;
}
name[j] = '\0';
//ch = '\0';
printf("Number %s\nName %s\n ",num,name);
j=0;
}
fclose(ifp); …Run Code Online (Sandbox Code Playgroud) 我编写此代码将文件'vetores'的内容复制到char矩阵,但我认为动态分配存在问题.有人可以帮帮我吗?
void cpyarqvetores( char** arqvetores, FILE* varquivo);
void freearqvetores( char** arqvetores );
int main()
{
FILE* varquivo;
varquivo = fopen("vetores.txt", "r");
char** arqvetores;
arqvetores = (char**) malloc(10*sizeof(char*));
cpyarqvetores( arqvetores, varquivo);
printf("%s\n", *(arqvetores + 4));//try prints this line
freearqvetores( arqvetores );
fclose( varquivo );
return 0;
}
//copy the contents of file 'vetores' to 'arqvetores'
void cpyarqvetores( char** arqvetores, FILE* varquivo){
char aux[440];
int strtam;
int i, j;
for( i = 0; i < 10; i++){
fgets( aux, 440, varquivo);
strtam …Run Code Online (Sandbox Code Playgroud) 好吧,当sizeof(Myenum)和sizeof(int)工作时,我想使用sizeof(对象),但我不想要对象的大小,而是指针的大小......只是为了便携性的原因,我需要要知道是否是64位指针或32位指针,我可以避免使用sizeof如果条件编译没问题,但我不知道是否有常量来检查我们是否在32位系统而不是64位
谢谢你的建议
对于家庭作业,我们正在开发CSV解析器.我正在尝试让事情奏效,但我遇到了一个问题.我似乎无法为结构中的"字段"值赋值.在他们提供的代码中,他们有:
typedef char f_string[MAX_CHARS+1] ; /* string for each field */
typedef struct {
int nfields; /* 0 => end of file */
f_string field[MAX_FIELDS]; /* array of strings for fields */
} csv_line ;
Run Code Online (Sandbox Code Playgroud)
使用上面定义的20和15的常量.看看它们有什么,struct hold和int,它包含一个数组,应该使用前面定义的f_string typedef填充它.好吧,很酷.我试着这样做:
f_string test = "Hello, Bob";
f_string testAgain = "this is dumb, k?";
f_string anArray[MAX_FIELDS] = {*test, *testAgain};
csv_line aLine;
aLine.nfields = 3;
aLine.field = *anArray;
Run Code Online (Sandbox Code Playgroud)
当我制作"anArray"时,如果我没有测试和testAgain的解释,我会收到关于在没有演员表的情况下对指针进行整数的警告.所以我把它们留在了.但是这条线:
aLine.field = *anArray;
Run Code Online (Sandbox Code Playgroud)
返回错误:"csv.c:87:错误:赋值中的不兼容类型"有或没有指针那么...所以我不确定我应该如何分配该变量?帮助将不胜感激!
如果我A在C++中有一个指向基类的指针,我怎么能在代码中告诉指针是派生类B还是C?
最近我看到以下片段代码:
if ((rgb = (fp16 *)malloc(width*height*sizeof (*rgb)*3)) == NULL)
Run Code Online (Sandbox Code Playgroud)
rgb被声明为某种类类型的指针.在上面的代码中,malloc()接受的参数是width*height*sizeof(*rgb)
所以它是某种自引用初始化(如果我可以通过给出这个名称来调用它!)即belore rgb指针由malloc分配,它在调用malloc时解除引用它.
在这个特定的代码中,我看到指针rgb没有初始化为NULL或任何东西.
这些代码的行为是什么?
正常运作或
由于空指针取消引用而导致崩溃,或者
车库指针取消引用
谢谢,
-广告.