简体   繁体   中英

C: Using strcpy to transfer one struct element to an array

Okay, so we're supposed to prompt a user to enter 25000 lines of text. Each line contains three integers each. We are then to pass the third integer in that line to another struct, and connect each integer until you have 25000 interconnected integers.

Here's what I've tried:

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

And there are warnings... "passing argument 1 of strcpy from incompatible pointer type" "passing argument 2 of strcpy makes pointer from integer without a cast" expected char*__ restrict __ but argument is of type 'int' plus when I inputted that 25000 lines of text, segmentation fault happens. Please help. Thank you!

strcpy is for copying strings (ie zero terminated byte char "arrays"), you maybe should use memcpy instead.

Or if you just want to assign a single integer to one element in the array, use plain assignment:

current->name[someIndex] = ecost;

Or, maybe you intend that the name member should be a string ? Then you should be using an array of characters and not integers. And you need to convert integer values to strings, using eg sprintf :

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

you can convert the integer to string using itoa and copy the string into root->name.

char str[20];

itoa(ecost, str, 10);

strcpy(root->name, str);

You did not state your exact issue so I am assuming you are overwhelmed and in that case you should try partitioning your implementation into functions so that you can work on isolated problems instead of a web of interconnected problems.

Here is one example:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM