第一和第二继续,第三返回NULL

时间:2019-06-15 16:09:30

标签: c null

该程序检查重复项,第一次它不告诉我该ID已经存在,但是第三次​​finddata函数返回null,无论它读取的内容是什么(在原始代码中,finddata是在while函数中)。

尝试了不同的id编译,更改了finddatta的类型(即使我想避免这种情况)并以不同的顺序触发函数

typedef struct{
    char name[MAXSTRING];
    int id;
} structure;
typedef struct node node;
typedef struct list list;

struct node {
    structure data;
    node* next;
};

struct list {
    node* head;
    int size;
};

list* list_create(){
    list* l=(list*) malloc(sizeof(list));
    assert(1);
    l->head=malloc(sizeof(node));
    l->size=0;
    return l;
}


node* finddata(int id,list* l){
        node* tmp=l->head;
        while(tmp->next!=NULL){
        if(tmp->data.id==id){
             return tmp;
        }
        tmp=tmp->next;
        }
        return NULL;
}


int main(int argc,char *argv[]){
    list* l=list_create();

    if(argc!=2){
        printf("Input Error\n");
    }

    node* found=(node*)malloc(sizeof(node));
    node* st=(node*)malloc(sizeof(node));

    if(found==NULL){
        printf("Error allocating memory\n");
        return 0;
    }

    printf("Give a name\n");
    scanf("%s",st->data.name);
    printf("Give id\n");
    scanf("%d",&st->data.id);

    found=finddata(st->data.id,l); 

    if(found!=NULL){
        printf("This id already exists\n");
    }          
    else{
        //Some Code
    }
    //Some more code
    return 0;
}

我希望发现不返回null。

1 个答案:

答案 0 :(得分:0)

public class MainActivity extends AppCompatActivity implements CakeAdapter.CakeClickedListener { RecyclerView mRecyclerView; TextView mCakeName; ImageView mCakeImage; TextView mCakeId; private List<CakesItem> mCakeList = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mRecyclerView = findViewById(R.id.cake_list_recycler_view); mRecyclerView.setHasFixedSize(true); GridLayoutManager mGridLayoutManager = new GridLayoutManager(MainActivity.this, 2); final CakeAdapter mCakeAdapter = new CakeAdapter(this); mRecyclerView.setLayoutManager(mGridLayoutManager); mRecyclerView.setAdapter(mCakeAdapter); mCakeAdapter.getCakeData(mCakeList); Retrofit retrofit = new Retrofit.Builder() .baseUrl(Constants.BASE_URL) .addConverterFactory(GsonConverterFactory.create()) .build(); BakingJsonApi bakingJsonApi = retrofit.create(BakingJsonApi.class); Call<List<CakesItem>> call = bakingJsonApi.getCakes(Constants.JSON_PATH); call.enqueue(new Callback<List<CakesItem>>() { @Override public void onResponse(Call<List<CakesItem>> call, Response<List<CakesItem>> response) { if (!response.isSuccessful()) { Toast.makeText(MainActivity.this, "Code: " + response.code(), Toast.LENGTH_SHORT).show(); return; } List<CakesItem> cakeItem = response.body(); mCakeAdapter.getCakeData(cakeItem); } @Override public void onFailure(Call<List<CakesItem>> call, Throwable t) { Toast.makeText(MainActivity.this, "Unable to load data" + t.getMessage(), Toast.LENGTH_SHORT).show(); } }); } public interface BakingJsonApi { @GET("/topher/2017/May/59121517_baking/{json}") Call<List<CakesItem>> getCakes(@Path("json") String path); } class Constants { static final String BAKING_API = "https://d17h27t6h515a5.cloudfront.net/topher/2017/May/59121517_baking/baking.json"; static final String BASE_URL = "https://d17h27t6h515a5.cloudfront.net/"; static final String JSON_PATH = "baking.json"; } 中,您为list_create分配了空间,但从未将节点的值设置为任何值。特别是node指针(next)尚未初始化。当您在l->head->next期间访问此文件时,可能会发生任何事情,从看起来可以运行的程序到多次运行,然后失败,甚至完全崩溃。

在尝试从分配的内存中读取所有值之前,请务必对其进行初始化。