簡體   English   中英

如何在C中將指針與結構一起使用

[英]How do I Use Pointers With Structs in C

嘿,我是堆棧溢出的新手,所以請對我的帖子提出建設性的批評!

我是一名剛接觸c的高中生(我之前使用過Java),並且對指針和結構有些困惑,尤其是在將它們作為函數之間的參數傳遞時。 我編寫了以下代碼,這些代碼可以編譯,但是在運行時出現分段錯誤(我認為這意味着內存與分配的空間重疊,如果我錯了,請糾正我)。 如果有人可以解釋為什么發生這種情況以及發生在哪里,那就太好了!

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct savedSite{
    char *siteName; 
    char *date; 
    int x;
} SAVED_SITE;

void printSite(struct savedSite site){
    printf("Site: %s\nDate Added: %s\nID:            
    % d\n",site.siteName,site.date,site.x);
}

SAVED_SITE* makeNewSite(){
    SAVED_SITE returnSite;
    printf("Enter Site Name");
    scanf("%s", returnSite.siteName);
    return &returnSite; 
}

int main() 
{   
    SAVED_SITE newSite;
    newSite = *makeNewSite();
    newSite.date = "3/13/2017";
    newSite.x = 89; 
    return 2;
}

謝謝!

編輯:我不滿意我在這里收到答案的速度! 非常感謝你們,這太不可思議了!

您的函數makeNewSite()導致分段錯誤。

SAVED_SITE* makeNewSite(){
    SAVED_SITE returnSite;
    printf("Enter Site Name");
    scanf("%s", returnSite.siteName);
    return &returnSite; 
}

變量returnSite是局部變量,在堆棧上創建。 函數調用結束后,該變量將被銷毀。 但是,您正在返回其地址並嘗試訪問它,這導致分段錯誤。

您可以嘗試以下方法:

SAVED_SITE* makeNewSite(){
    SAVED_SITE* returnSite = malloc(sizeof(SAVED_SITE));
    printf("Enter Site Name");
    scanf("%s", returnSite->siteName); // Not sure about this allocation
    return returnSite; 
}


int main() {   
    SAVED_SITE* newSite = makeNewSite(); // Get the pointer here.
    newSite->date = "3/13/2017";
    newSite->x = 89;
    free (newSite); 
    return 2;
}

在此代碼中,對malloc()的調用將在堆中而不是在堆棧中創建該結構,並且在函數調用之后它不會被破壞。

另請注意,我使用的是->而不是. 在主要功能上。 這是因為我有一個指向結構的指針,而不是結構本身。 newSite->date(*newSite).date

 #include<stdio.h>
 #include<string.h>
 #include<stdlib.h>
typedef struct savedSite{
char *siteName;
char *date;
int x;
} SAVED_SITE;
SAVED_SITE returnSite;
void printSite( SAVED_SITE* site){
printf("Site: %s\nDate Added: %s\nID:% d\n",site->siteName,site-   >date,site->x);
}

SAVED_SITE* makeNewSite(){
SAVED_SITE* returnSite = malloc(sizeof(SAVED_SITE));
printf("Enter Site Name");
scanf("%s", returnSite->siteName);
return returnSite;
}


int main() {
SAVED_SITE* newSite = makeNewSite(); // Get the pointer here.
newSite->date = "3/13/2017";
newSite->x = 89;
return 2;
}

我認為發生運行時錯誤是因為尚未為siteName和Date分配內存。 因此,當給它們分配一個字符串時,會出現運行時錯誤。 您可以通過malloc為它們分配內存,如下所示:

sitename = (char *)malloc(20 * sizeof(char));

或者只是在您的結構中獲得此內存:

char sitename[20];

您不應該為整個結構分配內存。 僅針對我提到的這些變量。 不要忘記釋放分配的內存。

這是您應該做的一些更改。

  • 您的版本中的函數makeNewSite()創建一個對象SAVED_SITE returnSite ,並返回其地址,該地址在銷毀該對象時將無效。 因此,我建議您分配一個永久存在的指針,如果它不是freedelete
  • char* siteName應該更改為固定大小的數組(或者如果需要,可以在運行時分配它),以便它有足夠的空間來存儲來自scanf數據
  • 我修改了printSite()以演示如何在函數中傳遞指針。

這是示例:

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

typedef struct savedSite{
    char siteName[128]; // 
    char *date; 
    int x;
} SAVED_SITE;

void printSite(struct savedSite *site){       // accept pointer as an argument
    printf("Site: %s\nDate Added: %s\nID:            
    % d\n",site->siteName,site->date,site->x);
}
SAVED_SITE* makeNewSite(){
    SAVED_SITE *returnSite = new SAVED_SITE; // allocate a struct that will live forever if not be deleted
    printf("Enter Site Name");
    scanf("%s", returnSite->siteName);
    return returnSite; // the pointer returned here is valid until you delete it
}
int main() 
{   
    SAVED_SITE *newSite; // create a pointer which doesn't point to anything
    newSite = makeNewSite(); // now the newSite* points to data struct
    newSite->date = "3/13/2017";
    newSite->x = 89; 
    //remember to delete the allocated memory when you don't need it anymore
    delete newSite;
    return 2;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM