小编mko*_*mko的帖子

C中的字符串赋值

我是C的新手.以下代码中的字符串赋值有效:

#include<stdio.h>
int main(void){
  char str[] = "string";
  printf("%s\n",str);
}
Run Code Online (Sandbox Code Playgroud)

但是在下面不起作用,即使我给索引号name[]:

#include <stdio.h>
int main(void){
  struct student {
    char name[10];
    int  salary;
  };
  struct student a;
  a.name[10] = "Markson";
  a.salary = 100;
  printf("the name is %s\n",a.name);
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

为什么会这样?

c string

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

如何在Python中评估集合中的两个或更多元素

s = set([1,2,3])
Run Code Online (Sandbox Code Playgroud)

我能做到这一点:

1 in s

#=> True
Run Code Online (Sandbox Code Playgroud)

我想知道没有使用循环,有没有办法做类似的事情:

1,2 in s

#=>True
Run Code Online (Sandbox Code Playgroud)

python

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

In C. the name of an array is an pointer to it's first element, isn't it?

In the following example code, I use string as array, if the name of str is an pointer to the first element of the str, in this case the first character of string, which is s, the size of str, should be 8, but it gives the 6 instead.

#include <stdio.h>

char str[] = "string";
char *ptr = "string";

int main(void){
    int first = sizeof(str);
    int second = sizeof(str[0]);
    int third = sizeof(ptr);
    int size_of_array = sizeof(str)/sizeof(str[0]);
    printf("the size is …
Run Code Online (Sandbox Code Playgroud)

c pointers

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

为什么将指向数组的指针传递给需要指向int的指针的函数,可以正常工作

出于好奇,我将行更改为set_array(&array1[0])以下内容set_array[array1],参数不是同一类型,但它有效,任何想法?

#include <stdio.h>
void set_array(int array[][9]);
int main(void) {
    int array1[4][9];
    for(int i = 0; i < 4; i++) {
        for(int j = 0; j < 9; j++) {
            array1[i][j] = j + 1;
        }
    }
    set_array(&array1[0]);
    for(int i = 0; i < 4; i++) {
        for(int j = 0; j < 9; j++) {
            printf("%d ", *(*(array1 + i) + j));
            //printf("%d ", array1[i][j]);
        }
        puts("\n");
    }
    return 0;

}
void set_array(int array[][9]) {
    for(int …
Run Code Online (Sandbox Code Playgroud)

c multidimensional-array

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

如何在一个语句中获得唯一的排序列表?

sort()的返回值为None,因此以下代码不起作用:

def sorted_unique_items(a):
   return list(set(a)).sort()
Run Code Online (Sandbox Code Playgroud)

想要更好的解决方案吗?

python

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

python中的正则表达式组重复

string = "input-ports 6012, 6017, 6016"
m = re.match("input-ports(\s\d{4},?)(\s\d{4},?)(\s\d{4},?)", string)
print m.groups #=> (' 6012,', ' 6017,', ' 6016')
Run Code Online (Sandbox Code Playgroud)

但是当我想使用组重复时,它只返回最后一个数字

m = re.match("input-ports(\s\d{4},?)+", string)
print m.groups #=> (' 6016',)
Run Code Online (Sandbox Code Playgroud)

谁能告诉我这是为什么?

python regex

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

在heroku上部署rails应用程序

嘿,我害怕我应该问一个菜鸟问题:

将我的应用程序推送到heroku后.我得到没有数据库的错误

这是我使用的命令

heroku rake db:migrate

我的应用程序可以在本地运行没有问题,但我只在开发中注意到数据库文件.而我的测试环境只使用rails serverlocalhost:3000

有人告诉我如何在heroku中以生产模式创建数据库.

谢谢

这是heroku日志文件:

这是日志

开始GET"/鼓手/ 1"为221.9.247.14在星期六12月18日06:17:40 -0800 2010由DrummersController处理#show as HTML参数:{"id"=>"1"}在167ms完成

ActiveRecord :: RecordNotFound(无法找到ID = 1的Drummer):app/controllers/drummers_controller.rb:11:在`show'中

我认为可能是由于datebase,配置文件,我在本地测试中使用sqlite3,并且所有迁移文件都是开发前缀,

deployment ruby-on-rails heroku

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

对Rack的源代码中的变量类型感到困惑

我的问题与以下3个代码摘录有关:

类方法代码: start(options = nil)

# File 'lib/rack/server.rb', line 136

def self.start(options = nil)
  new(options).start
end
Run Code Online (Sandbox Code Playgroud)

实例方法的代码: #initialize(options = nil)

# File 'lib/rack/server.rb', line 174
def initialize(options = nil)
  @options = options
  @app = options[:app] if options && options[:app]
end
Run Code Online (Sandbox Code Playgroud)

实例方法的代码: #start

# File 'lib/rack/server.rb', line 229
def start
  if options[:warn]
    $-w = true
  end

  ...# more lines that are not related to my question
end
Run Code Online (Sandbox Code Playgroud)

我的问题是,options实例方法中的局部变量应该start@options吗?在我的选项中,由于前2个摘录显示选项作为参数传递给initialize,并使其成为实例变量@options,所以在实例方法启动时,它应该引用它@options,而不是options …

ruby rack scope

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

无法在addEventListener方法的第二个参数中添加括号

这让我很困惑

var show = function(){
    console.log('wow');
};

var show2 = function(word){
    console.log(word);
};

button_element.addEventListener('click', show2('wow'), false)
Run Code Online (Sandbox Code Playgroud)

'哇'//它会立即返回字符串但按下按钮,控制台中没有任何输出,

button_element.addEventListener('click', show. false)
Run Code Online (Sandbox Code Playgroud)

没有像我们期望的那样返回字符串,然后wow按照预期在控制台中按下按钮

任何人解释为什么这样做?

javascript

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

为什么要在Ruby的方法定义中添加&b参数呢?

def show a, &b
yield a
end

show 1 {|x| puts x}
Run Code Online (Sandbox Code Playgroud)

但是在定义中没有&b,代码也有效,所以我想知道在什么情况下,&b是必需的?

ruby

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