C(Linux)的结构删除元素的传球达阵 [英] C (linux) passing array of structs to delete element

查看:108
本文介绍了C(Linux)的结构删除元素的传球达阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的C程序如下问题,

I have the following issues in my C Program,

首先,我有以下的结构,

FIRST, I have the following structure,

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

struct ts{
  char *fname;
  char *lname;
  char *fingers;
  char *toes;
};

void delelement(char *, struct ts *);
int i;

int main(int argc, char **argv){
  struct ts *ex=(struct ts*)malloc(sizeof(struct ts));

  ex[0].fname="joe";
  ex[0].lname="bob";
  ex[0].fingers="11";
  ex[0].toes="9";

  ex[1].fname="billy";
  ex[1].lname="bronco";
  ex[1].fingers="10";
  ex[1].toes="10";

  ex[2].fname="martha";
  ex[2].lname="sue";
  ex[2].fingers="12";
  ex[2].toes="20";

  delelement("billy", ex);

  return 0;
}

对于通过结构数组中的调试我环路和打印值 - 这工作(请不要介意我不是在这个函数返回一个值 - 我遇到了这个问题之前,我们甚至达到那个)​​<。 / p>

For debugging I loop through and print the values in the array of structs - this works (nevermind I'm not returning a value in this function - the problem I'm running into is before we even get to that).

void delelement(char *delwhat, struct ts *passedex){

  //struct ts *tempex=(struct ts*)malloc(sizeof(struct ts));
  for(i=0; i<sizeof(passedex)-1; i++){
    printf("passedex[%d].fname is %s\n", i, passedex[i].fname);    
    printf("passedex[%d].lname is %s\n", i, passedex[i].lname);
    printf("passedex[%d].fingers is %s\n", i, passedex[i].fingers);
    printf("passedex[%d].toes is %s\n", i, passedex[i].toes);
  }
  return;
}

现在工作正常 - 正常打印出的信息

now THAT works fine - prints out information correctly.

现在让我们来简单地删除注释,并定义结构的临时数组

now let's simply remove the comment and define the temporary array of structs

void delelement(char *delwhat, struct ts *passedex){

  struct ts *tempex=(struct ts*)malloc(sizeof(struct ts));
  for(i=0; i<sizeof(passedex)-1; i++){
    printf("passedex[%d].fname is %s\n", i, passedex[i].fname);    
    printf("passedex[%d].lname is %s\n", i, passedex[i].lname);
    printf("passedex[%d].fingers is %s\n", i, passedex[i].fingers);
    printf("passedex[%d].toes is %s\n", i, passedex[i].toes);

  }
  return;
}

BOOM - 段错误

passedex[0].fname is joe
passedex[0].lname is bob
passedex[0].fingers is 11
passedex[0].toes is 9
passedex[1].fname is billy
Segmentation fault

行,所以我尝试了不同的方法 - 哪种作品

OK so I tried a different approach - which kind of works

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct ts{
  char *fname;
  char *lname;
  char *fingers;
  char *toes;
};
void delelement(char *, struct ts *, struct ts *);
int i;
int main(int argc, char **argv){
  struct ts *ex=(struct ts*)malloc(sizeof(struct ts));
  struct ts *tempex=(struct ts*)malloc(sizeof(struct ts));

  ex[0].fname="joe";
  ex[0].lname="bob";
  ex[0].fingers="11";
  ex[0].toes="9";
  ex[1].fname="billy";
  ex[1].lname="bronco";
  ex[1].fingers="10";
  ex[1].toes="10";
  ex[2].fname="martha";
  ex[2].lname="sue";
  ex[2].fingers="12";
  ex[2].toes="20";
  delelement("billy", ex, tempex);
  return 0;
}
void delelement(char *delwhat, struct ts *passedex, struct ts *tempex){
  //struct ts *tempex=(struct ts*)malloc(sizeof(struct ts));
  for(i=0; i<sizeof(passedex)-1; i++){
    printf("passedex[%d].fname is %s\n", i, passedex[i].fname);    
    printf("passedex[%d].lname is %s\n", i, passedex[i].lname);
    printf("passedex[%d].fingers is %s\n", i, passedex[i].fingers);
    printf("passedex[%d].toes is %s\n", i, passedex[i].toes);
  }
  return;
}

正常工作......(tempex现在主要定义)

WORKS fine... (tempex now defined in main)

passedex[0].fname is joe
passedex[0].lname is bob
passedex[0].fingers is 11
passedex[0].toes is 9
passedex[1].fname is billy
passedex[1].lname is bronco
passedex[1].fingers is 10
passedex[1].toes is 10
passedex[2].fname is martha
passedex[2].lname is sue
passedex[2].fingers is 12
passedex[2].toes is 20

现在,让我们开始赋值为* tempex - 与tempex没有段错误的主要定义

now lets start assigning values to *tempex - no segfault with tempex defined in main

void delelement(char *delwhat, struct ts *passedex, struct ts *tempex){

  //struct ts *tempex=(struct ts*)malloc(sizeof(struct ts));

  for(i=0; i<sizeof(passedex)-1; i++){
    printf("passedex[%d].fname is %s\n", i, passedex[i].fname);    
    printf("passedex[%d].lname is %s\n", i, passedex[i].lname);
    printf("passedex[%d].fingers is %s\n", i, passedex[i].fingers);
    printf("passedex[%d].toes is %s\n", i, passedex[i].toes);
    tempex[i].fname=passedex[i].fname;
    tempex[i].lname=passedex[i].lname;
    tempex[i].fingers=passedex[i].fingers;
    tempex[i].toes=passedex[i].toes;
  }
  return;
}

但现在 - 怪事

but NOW - weirdness

passedex[0].fname is joe
passedex[0].lname is bob
passedex[0].fingers is 11
passedex[0].toes is 9
passedex[1].fname is billy
passedex[1].lname is bronco
passedex[1].fingers is joe
passedex[1].toes is bob
passedex[2].fname is 11
passedex[2].lname is 9
passedex[2].fingers is billy
passedex[2].toes is bronco

我在这里得到错误。该目标是有包含字符*的结构的动态数组。一旦越过这个问题上,会有主(或地方),我想删除这些结构的一个实例。

I am getting errors here. The goal is to have a dynamic array of structures containing char *'s. Once past this issue, there will be an instance in main (or wherever) that I wish to delete one of those structures.

我正想为是像

struct ts* delelement(char *delwhat, struct ts *passedex, struct ts *tempex){

  //struct ts *tempex=(struct ts*)malloc(sizeof(struct ts));

  for(i=0; i<sizeof(passedex)-1; i++){
    printf("passedex[%d].fname is %s\n", i, passedex[i].fname);    
    printf("passedex[%d].lname is %s\n", i, passedex[i].lname);
    printf("passedex[%d].fingers is %s\n", i, passedex[i].fingers);
    printf("passedex[%d].toes is %s\n", i, passedex[i].toes);
    //load tempex with everything except the one I want to delete
    if(!(passedex[i].fname==delwhat)){
      tempex[i].fname=passedex[i].fname;
      tempex[i].lname=passedex[i].lname;
      tempex[i].fingers=passedex[i].fingers;
      tempex[i].toes=passedex[i].toes;
    }
  }
  free(passedex); //haven't got here yet - dunno if needed
  //realloc if needed - gotta get here first - pass segfault and/or jumbled data

  for(i=0; i<sizeof(passedex)-1; i++){
    passedex[i].fname=tempex[i].fname;
    passedex[i].lname=tempex[i].lname;
    passedex[i].fingers=tempex[i].fingers;
    passedex[i].toes=tempex[i].toes;
  }

  return passedex;
}

因此​​,这将产生(或)结构与...加载阵列减去一个被删除的工作的一个临时数组...重装结构的传递的数组并传递回来。

So it would create (or have) a temporary array of structs to work with... load that array minus the one to be deleted... reload the passed array of structs and pass it back.

推荐答案

您只分配了一个结构,你想象不3。

You only allocated one struct, not 3 as you think.

您应该做的是这样的:

struct ts *ex = malloc( sizeof(struct ts) * 3 );

你要做的就是与为const char (字符串)分配确定。但是,你应该改变你的结构TS 来定义的:

What you do is OK with the const char (string literal) assignments. But you should change your definition of struct ts to:

struct ts{
  const char *fname;
  const char *lname;
  const char *fingers;
  const char *toes;
};

这样,你的编译器会警告你,如果你试图改变这些字符串的内容。

This way your compiler will warn you if you try to change the contents of those strings.

否则,您可以使用的malloc()的strcpy()的char *的在结构。而

Otherwise you can use malloc() and strcpy() for your char*'s in the struct.And

ex[0].fname = malloc(sizeof(char) * 128);
//...
strcpy(ex[0].fname , "myString");
//...

还有:

这code片段看起来有点怪异

This code snippet looks a bit weird

void delelement(char *delwhat, struct ts *passedex){

  for(i=0; i<sizeof(passedex)-1; i++){
      //...
  }
  return;
}

我想你的意思

void delelement(char *delwhat, struct ts *passedex , int array_size)
{
    int i;
    for( i=0 ; i < array_size ; i++ )
    {
         //...

您需要将数组的大小作为参数传递。你会发现这个有趣的链接上一些帖子:关于malloc和sizeof的 新手问题

You need to pass the size of the array as an argument. You may find some posts on this link interesting : newbie questions about malloc and sizeof

这篇关于C(Linux)的结构删除元素的传球达阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆