当我使用动态分配时,所有值都相同

时间:2018-05-15 23:38:24

标签: c

我尝试从用户那里获取文字,直到他输入apply然后打印出来。我想在每个单词之后动态地为列表分配一个大小(通过使用struct):

base_df <- data.frame(id = c("a", "a", "a", "a", "a", "b", "b", "b", "b", "b"), 
                      var1 = c(1,3,5,-7,-9,11,13,15,17,19),
                      var2 = c(2,4,6,8,-10,12,14,16,18,20))

compare_df <- data.frame(id = c("a", "b"), 
                     xx = c("1.5_2.5", "17.5_18.5"),
                     yy = c("3.5_2.5", "12.5_18.5"),
                     zz = c("3.5_4.5", "15.5_16.5"))

# desired
final_df <- data.frame(
  id = c("a", "a", "a", "a", "a", "a", "a", "b", "b", "b", "b", "b", "b", "b"),
  ind = c("org", "xx", "org", "zz", "org", "org", "org", "org", "org", "org", "zz", "org", "xx", "org"),
  var1 = c(1, 1.5, 3, 3.5, 5, -7, -9, 11, 13, 15, 15.5, 17, 17.5, 19),
  var2 = c(2, 2.5, 4, 4.5, 6, 8, -10, 12, 14, 16, 16.5, 18, 18.5, 20))

output 为什么会这样?

1 个答案:

答案 0 :(得分:1)

您正在错误地分配字符串:

list.words[list.length - 1] = str;

这只会将list.words指针指向str,而str会被用户输入的最新单词覆盖。你需要

strcpy(list.words[list.length - 1], str);