尝试访问struct中的字符串会导致分段错误

时间:2017-04-29 15:45:59

标签: c struct

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

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

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

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);}

1 个答案:

答案 0 :(得分:0)

考虑一下。我在malloc之前移动了语句OpeningAddress = "google";并将其更改为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;}