当我在结构中定义字符类型时,它似乎需要超过1个字节; 实际上它似乎需要4个字节.
以下是我的计划:
#include <stdio.h>
int main(void)
{
struct book{
char name;
float price;
int pages;
};
struct book b1={'B',130.00,550};
printf("\nAddress of structure:%u",&b1);
printf("\nAddress of character name:%u",&b1.name);
printf("\nAddress of float price:%u",&b1.price);
printf("\nAddress of integer pages:%u",&b1.pages);
printf("\n\n");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我运行上面的程序时,我得到以下输出:
Address of structure:557762432
Address of character name:557762432
Address of float price:557762436
Address of integer pages:557762440
Run Code Online (Sandbox Code Playgroud)
为什么我看到变量"name"的地址和变量"price"之间的4个字节的差异?
运行该程序的系统是运行Fedora-14的x86_64位.
我想使用如下字典:示例:{[8,16]:[[1,2,4,8],8],[16,24]:[[1,2,3,4,8, 12],12]}
8和16是将要输入的两个数字,我需要构建如上所述的字典.
使用setdefault,我可以在字典中创建值列表,但不能为键创建列表
以下是我的代码:
#!/usr/bin/env python
"""
This Program calculates common factors between two Numbers , which
is stored on a list and also greatest common factor is also computed.
All this is stored in a dictionary
Example: { '[n1, n2]': [[Commonfac1(n1,n2), Commonfac2(n1,n2)....Commonfacn(n1,n2)],GreatestCommonFactor] }
"""
def Factors(number):
result = []
for i in range(1, number+1):
if (number % i) == 0:
result.append(i)
return result
def Common_Factors(n1, n2):
result = []
for element in n1:
if element in n2:
result.append(element) …Run Code Online (Sandbox Code Playgroud)