小编Ste*_*ocy的帖子

为什么这个链表从上次输入打印?C链表程序

所以我有这个简单的链表程序:

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

typedef struct record record;
struct record {
    char name[32]; 
    float score;
};

typedef struct node node;
struct node {
    record data;
    node *next; 
}; 

int main() {
    char data[100];
    char name[32];
    float x;
    node *p, *head;
    int counter = 0;

    head = 0;

    while ((fgets(data, 60, stdin) != NULL) && counter <= 3) {
        p = (node *) malloc (sizeof(node));

        sscanf(data, "%s %f", name, &x);
        strcpy(p->data.name,name);
        p->data.score = x;
        p->next = head;
        head = p;

        counter++; …
Run Code Online (Sandbox Code Playgroud)

c linked-list

2
推荐指数
1
解决办法
89
查看次数

无论我改变什么,C程序输出相同的数字

所以我有一个奇怪的问题,无论.c文件,编译器,IDE,或者我改变输出的内容每次都是一样的.起初我正在为我的课写一个简单的程序,

#include <stdio.h>

int main () {

    int n1, n2, res;
    printf("Enter two numbers to divide.\n");
    scanf("%d", &n1);
    scanf("%d", &n2);

    res = n1/n2;

    if (n2 == 0) {

        printf("You cannot divide by 0!\n");

    } else {

        printf("Result: %d", &res);
        printf("\n");

    }

    system("PAUSE");
}
Run Code Online (Sandbox Code Playgroud)

我的结果总是等于数字6422276.我试图创建一个新的文件,简单地初始化两个整数来12分别,并告诉编译器将它们加在一起.输出等于6422276.

我该怎么办?

c

2
推荐指数
1
解决办法
79
查看次数

如何将包含html文本的字符串放在javascript函数中?

如何将包含html文本的字符串放在JavaScript函数中?

String test = "DisplayNext(\"String containing html tags\")";

String NextLink = "<br><a href='#' onclick="+test+"> Next</a>";
Run Code Online (Sandbox Code Playgroud)

JavaScript函数:

function DisplayNext(Next){
    alert(Next);
}
Run Code Online (Sandbox Code Playgroud)

我想将html标签传递给JavaScript函数并使用警告框显示它.我该怎么做?

html javascript java

1
推荐指数
1
解决办法
1万
查看次数

使用递归函数反转打印链表

我刚试了一个程序来反转打印一个单一的链表.

假设链表已准备好5个元素: 1->2->3->4->5

我编写了一个程序,以相反的顺序打印,如:5 4 3 2 1
但我的程序打印为5 4 3 2; 在1不打印.为什么?

int Reverse_List(abc_t *pNode) {
    abc_t *pTemp;
    int count = 5;

    if (pNode->pNext != NULL) {
        pNode = pNode->pNext;
        Reverse_List(pNode);
        printf("The node is %d\n", pNode->a);
    }
}
Run Code Online (Sandbox Code Playgroud)

c recursion singly-linked-list

1
推荐指数
1
解决办法
4619
查看次数

为什么选择复制构造函数而不是移动构造函数

我正在查看以下有关移动构造函数/赋值的示例:https: //msdn.microsoft.com/en-us/library/dd293665.aspx

我通过添加交换函数来修改它,以简化移动构造函数/赋值和复制赋值:

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

class MemoryBlock
{
public:

    // Simple constructor that initializes the resource.
    explicit MemoryBlock(size_t length)
        : _length(length)
        , _data(new int[length])
    {
        std::cout << "In MemoryBlock(size_t). length = "
                  << _length << "." << std::endl;
    }

    // Destructor.
    ~MemoryBlock()
    {
        std::cout << "In ~MemoryBlock(). length = "
                  << _length << ".";

        if (_data != nullptr)
        {
            std::cout << " Deleting resource.";
            // Delete the resource.
            delete[] _data;
        } …
Run Code Online (Sandbox Code Playgroud)

c++ copy-constructor move-constructor move-semantics c++11

1
推荐指数
1
解决办法
91
查看次数

NGRX效果仅在多次调度时触发一次成功操作

我正在开发一个相对简单的应用程序,我想了解更多有关ngrx,redux和angular 2的信息.

我将简要介绍一下我的app,appstate和reducer的设置.

在我的应用程序中,我想使用webgl框架在我的屏幕上绘制某些对象(网格).我想通过创建具有这些网格属性的简单对象,并通过ngrx存储添加网格,并将其保存在应用程序状态中.每当添加"网格"时,我想使用ngrx sideeffects在屏幕上使用可访问webgl框架的服务绘制网格.

我的(简化)appstate看起来像这样:

export interface AppState {
    meshList: MeshListReducer.MeshListState
}
Run Code Online (Sandbox Code Playgroud)

要添加网格,我创建了以下reducer函数:

case MeshActions.ADD_MESH: {
    return Object.assign({
        entities: [meshAction.payload, ...state.entities]
    });
}

case MeshActions.ADD_MESH_SUCCESS:
case MeshActions.UPDATE_MESH: {
   // This reducer function is only being called once, see below for further explanation.
   return Object.assign({}, state, {
        entities: state.entities.map(meshItem => {
            // If we can find a mesh with this id, use the sub reducer to update it.
            if (meshItem.uuid === meshAction.payload.uuid)
                 return meshSubReducer(meshItem, meshAction);

            return meshItem;
            })
        });
    } …
Run Code Online (Sandbox Code Playgroud)

typescript redux ngrx angular angular5

1
推荐指数
1
解决办法
3146
查看次数

试图在C中打印字符数组

我试图在C中打印字符数组,但如果出现任何空格,则不打印数组.这是我的代码:

int main() {
    char str[100];
    int len = 0;

    printf("Enter the string");
    scanf("%s", str);
    len = strlen(str);

    int i = 0;
    for (i = 0; i < len; i++) { 
       printf("%s", str[i]); 
    }    

    getch();
}
Run Code Online (Sandbox Code Playgroud)
Input: Bangalore is in India
Output: Bangalore
Run Code Online (Sandbox Code Playgroud)

有什么建议?

c

0
推荐指数
1
解决办法
2971
查看次数

如何在C中漂亮打印数组

所以我有这个问题,我将打印数组.
输出应该是这样的:

{element0,element1,element2}

所以我写了这个:

#include "stdio.h"

main() {
    int a[10] = {1, 0, 3, 10, 20, 0, 7, 8, 15, 14};
    int i;

    for (i = 0; i < 10; i++) {
        printf("{%d, }", a[10]);
    }
}
Run Code Online (Sandbox Code Playgroud)

好吧,我怎么能在世界上写出一行中的所有数字,它们应该在大括号之间,用逗号分隔.我假设你应该使用一些指向所有数字的指针.但我是新手,从来没有学过这个,而且相当棘手......

是的,但我想我应该使用指针,如果我没有弄错.

c arrays

0
推荐指数
1
解决办法
328
查看次数

检查malloc'ed指针是否为NULL

我有以下代码(这里简化).

#include <stdio.h>
#include <stdlib.h>
struct inner {
    int a;
    int b;
};

struct outer {
    struct inner *next;
    struct inner *prev;

};


int main(int argc, char *argv[]) {
    struct outer *c = malloc(sizeof(struct outer));

    if (c->next) {
        printf("here\n");
    }

    if (c->next != NULL) {
        printf("here too");
    }
}
Run Code Online (Sandbox Code Playgroud)

如您所见,c仅分配了内存,因此c->next并且c->prev是NULL地址.但两份印刷报表仍然有效.if如果c->next不是空指针,我如何使语句工作?

c struct pointers

0
推荐指数
2
解决办法
344
查看次数

将数组的指针传递给要在C中的printf`中使用的函数

我遇到了分段错误的麻烦,似乎是我试图printf()用来取消引用单个数组值,而不是使用指向我要打印的内存位置的指针.

如果我在一个函数中定义一个数组,并希望在包含的循环中传递该数组以通过char引用char printf(),我应该如何传递和引用该数组?

void arrayFunc(){

  char array [5];
  array[0] = 'a';
  array[1] = 'b';
  array[2] = 'c';
  array[3] = 'd';
  array[4] = 'e';

  printArray(array);
}

void printArray(char* array1){
  int i;
  for(i = 0; i < 5; i++){
        printf("%c", array1 + i);
  }
}
Run Code Online (Sandbox Code Playgroud)

似乎没有完成工作.

c memory arrays segmentation-fault

0
推荐指数
1
解决办法
49
查看次数