gpt4 book ai didi

c - 尝试访问结构中的字符串会导致段错误

转载 作者:行者123 更新时间:2023-11-30 19:34:35 25 4
gpt4 key购买 nike

这是我第一次在这里发布问题,我会尽力做到精确。所以我有这个项目,当我尝试访问结构中的字符串时,它给了我一个段错误。

struct InfoSession {
TabNodePtr FirstTab;
TabNodePtr LastTab;
TabNodePtr CurrTab;
TabNodePtr AuxTab;
char* OpeningAddress;};

这是 Session.c 文件中包含的结构,其中包含我想在 main 中调用的所有函数。

void SessionNewOpeningAddress(char * OpeningAddress,InfoSessionPtr Session){
scanf("%s",&OpeningAddress);
strcpy(Session->OpeningAddress,OpeningAddress);}

这是导致段错误的函数之一(经过测试,我意识到每个调用 Session->OpeningAddress 的函数都会导致段错误。)这是我的 main.c,其中我的 main已包含在内。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "Session.h"



int main(int argc, char *argv[]) {
InfoSessionPtr MySession=NULL;
int option;
char OpeningAddress;


do{

printf("\n\n1. Create Session.\n\
2. Create new tab.\n\
3. Go to next tab.\n\
4. Go to previous tab.\n\
5. Move tab left.\n\
6. Move tab right.\n\
7. Set new opening address.\n\
8. Show the default opening address.\n\
9. Go to next site.\n\
10.Go to previous site.\n\
11.Show the current address.\n\
12.Close the current tab.\n\
13.Close session.\n\
14.Open a new address.\n\
Dwste thn epilogh sas(1-14, 0 gia eksodo):");

scanf("%d",&option);
switch(option)
{case 1:
SessionNew(&OpeningAddress);
MySession =&SessionNew;
printf("The Session has been created!");
break;
case 2:
SessionNewTab(MySession);
break;
case 3:
SessionTabNext(MySession);
break;
case 4:
SessionTabPrev(MySession);
break;
case 5:
SessionTabMoveLeft(MySession);
break;
case 6:
SessionTabMoveRight(MySession);
break;
case 7:
printf("Dwste nea dieuthinsi gia default\n");
SessionNewOpeningAddress(&OpeningAddress,MySession);
break;
case 8:
SessionShowOpeningAddress(MySession);
break;
case 9:
SessionSiteNext(MySession);
break;
case 10:
SessionSitePrev(MySession);
break;
case 11:
SessionAddressShow(MySession);
break;
case 12:
SessionTabClose(MySession);
break;
case 13:
SessionClose(MySession);
break;
case 14:
SessionNewAddress(&OpeningAddress,MySession);
break;
}
}while(option);

return 0;}

我已经在互联网上进行了搜索,但我仍然不明白问题出在哪里。任何可以帮助我的人请尽快回复。哦,SessionNew 函数在这里(这个函数中一切正常):

InfoSessionPtr SessionNew(char * OpeningAddress){
TabNodePtr Tab;
InfoSessionPtr IS;
SiteNodePtr Site;
IS=malloc(sizeof(struct InfoSession));
Tab=malloc(sizeof(struct TabNode));
Site=malloc(sizeof(struct SiteNode));
IS->OpeningAddress=malloc(strlen(OpeningAddress)+1);
Site->Address=malloc(strlen(OpeningAddress)+1);
IS->AuxTab = NULL;
IS->CurrTab = Tab;
IS->FirstTab = Tab;
IS->LastTab = Tab;
Tab->NextTab = Tab;
Tab->PrevTab = Tab;
Site->UpTab = Tab;
Tab->CurrSite = Site;
Tab->FirstSite = Site;
OpeningAddress = "google";
strcpy(IS->OpeningAddress,OpeningAddress);
strcpy(Site->Address,IS->OpeningAddress);}

最佳答案

考虑一下这一点。我将语句 OpeningAddress = "google"; 移到了 malloc 之前,并将其更改为 strcpy。由于我没有 session.h 文件和其他函数,因此我删除了这些引用。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct InfoSession {
char* OpeningAddress;};

typedef struct InfoSession *InfoSessionPtr;

void SessionNewOpeningAddress(char * OpeningAddress,InfoSessionPtr Session){
scanf("%39s",OpeningAddress);
strcpy(Session->OpeningAddress,OpeningAddress);}

InfoSessionPtr SessionNew(char * OpeningAddress){
InfoSessionPtr IS;
IS=malloc(sizeof(struct InfoSession));

strcpy ( OpeningAddress, "google");

IS->OpeningAddress=malloc(strlen(OpeningAddress)+1);
strcpy(IS->OpeningAddress,OpeningAddress);
return IS;}

int main(int argc, char *argv[]) {
InfoSessionPtr MySession=NULL;
int option;
char OpeningAddress[40] = "g";


do{

printf("\n\n1. Create Session.\n\
2. Create new tab.\n\
3. Go to next tab.\n\
4. Go to previous tab.\n\
5. Move tab left.\n\
6. Move tab right.\n\
7. Set new opening address.\n\
8. Show the default opening address.\n\
9. Go to next site.\n\
10.Go to previous site.\n\
11.Show the current address.\n\
12.Close the current tab.\n\
13.Close session.\n\
14.Open a new address.\n\
Dwste thn epilogh sas(1-14, 0 gia eksodo):");

scanf("%d",&option);
switch(option)
{case 1:
MySession = SessionNew(OpeningAddress);
printf("The Session has been created!");
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
break;
case 7:
printf("Dwste nea dieuthinsi gia default\n");
SessionNewOpeningAddress(OpeningAddress,MySession);
break;
case 8:
break;
case 9:
break;
case 10:
break;
case 11:
break;
case 12:
break;
case 13:
break;
case 14:
break;
}
}while(option);

return 0;}

关于c - 尝试访问结构中的字符串会导致段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43697569/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com