簡體   English   中英

C:使用strcpy將一個struct元素傳輸到數組

[英]C: Using strcpy to transfer one struct element to an array

好的,因此我們應該提示用戶輸入25000行文本。 每行包含三個整數。 然后,我們將該行中的第三個整數傳遞給另一個結構,並連接每個整數,直到您擁有25000個互連的整數。

這是我嘗試過的:

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

typedef struct graph{
    int begin;
    int end;
    int cost;
} PathEdge;
int comp_fcn(const void *a, const void *b) {
    return ((PathEdge *) a)->cost - ((PathEdge *) b)->cost;
}
int main(void)
{
    int nlines,i;
    char r;
    int ecost,ebegin,eend;
    scanf("%d",&nlines);
    PathEdge edges[nlines+1];
    for(i=0;i<nlines;i++)
    {
        scanf("%d, %d, %dn",&ebegin, &eend, &ecost);
        edges[i].begin = ebegin;
        edges[i].end = eend;
        edges[i].cost = ecost;
        struct town
        {
            struct town *north;
            int name[25000];
        };
        struct town *root, *current;
        root = malloc(sizeof(struct town));
        root->north = NULL;
        strcpy (root->name,ecost);
        current = malloc(sizeof(struct town));
        current->north = root;
        strcpy (current->name,ecost);
    }
    printf("Please enter a node that you want to examine. If you want to    exit, please press 'X'.n");
    scanf("%c",&r);
    switch(r)
    {
        case 'X':
        case 'x':
        printf("You entered a wrong value. Gomen. Try againn.");
        break;
        default:
        if((0<r)&&(r<25000))
        {
            printf("You have accessed node %dn",r);
            printf("Its neighboring nodes are %dn",edges[r].cost);
            printf("Its neighboring nodes are %dn",edges[i].cost);
        }
        else
        {
            printf("Invalid input again. Please do try again. Thanksn");
        }
        break;
    }
    return 0;
}

並有警告...“從不兼容的指針類型傳遞strcpy的參數1”“從字符串兼容性傳遞strcpy的參數2使指針從整數轉換而無需強制轉換”預期的char * __限制__,但是當我輸入時參數的類型為'int' 25000行文本,出現分段錯誤。 請幫忙。 謝謝!

strcpy用於復制字符串 (即零終止字節char “數組”),您可能應該使用memcpy代替。

或者,如果您只想為數組中的一個元素分配一個整數,請使用普通分配:

current->name[someIndex] = ecost;

或者,也許您打算將name成員設為字符串 然后,您應該使用字符數組而不是整數。 並且您需要使用sprintf將整數值轉換為字符串:

sprintf(current->name, "%d", ecost);

您可以使用itoa將整數轉換為字符串,然后將字符串復制到root-> name。

char str[20];

itoa(ecost, str, 10);

strcpy(root->name, str);

您沒有說明確切的問題,所以我假設您不知所措,在這種情況下,您應該嘗試將實現划分為功能,以便可以處理孤立的問題而不是互連的問題。

這是一個例子:

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

typedef struct graph {
    int begin;
    int end;
    int cost;
} PathEdge;

const char * GenerateInput()
{
    static char local[2000];
    static int last = 0;
    int a, b, c;
    a = last++;
    b = last++;
    c = last++;
    sprintf_s(local, 2000, "%i %i %i", a, b, c);
    return local;
}

void PathEdgeInitializeFromString(PathEdge * edge, const char * str)
{
    sscanf_s(str, "%d %d %dn", &edge->begin, &edge->cost, &edge->end);
}

void QueryAndPrint(PathEdge * edges, int edges_n)
{
    printf("Enter a number from 1 to %i: ", edges_n);
    int index = 0;
    scanf_s("%i", &index);
    --index;
    if (index < 0 || !(index < (edges_n)))
        printf("Error");
    else
        printf("%i, %i, %i\n", edges[index].begin, edges[index].cost, edges[index].end);
}

int main() {
    PathEdge edges[25000];

    for (int i = 0; i < 25000; ++i)
    {
        const char * line = GenerateInput();
        PathEdgeInitializeFromString(edges + i, line);
    }

    QueryAndPrint(edges, 25000);

    return 0;
}

暫無
暫無

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

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