我正在尝试编写一个将字母转换为数字的代码.例如,A ==> 0 B ==> 1 C ==> 2,依此类推.我想写下26条陈述.我想知道是否有更好的方法来做到这一点......
谢谢!
int length = strlen(src);
char *structSpace = malloc(sizeof(String) + length + 1);
String *string = (String*) structSpace;
int *string = (int*) structSpace;
Run Code Online (Sandbox Code Playgroud)
*我创建了一个名为String的结构
我是编程新手,我想写一个Windows应用程序.到目前为止,我已经阅读了24小时内自学C教学,我应该做什么(阅读教程)旁边完成我的目标?谢谢
我已经分配了一些内存,char* memoryChunk = malloc ( 80* sizeof(char) + 1);是什么阻止我写入超过81个单元的内存位置?我该怎么做才能防止这种情况发生?
void testStage2(void) {
char c_str1[20] = "hello";
char* ut_str1;
char* ut_str2;
printf("Starting stage 2 tests\n");
strcat(c_str1, " world");
printf("%s\n", c_str1); // nothing exciting, prints "hello world"
ut_str1 = utstrdup("hello ");
ut_str1 = utstrrealloc(ut_str1, 20);
utstrcat(ut_str1, c_str1);
printf("%s\n", ut_str1); // slightly more exciting, prints "hello hello world"
utstrcat(ut_str1, " world");
printf("%s\n", ut_str1); // exciting, should print "hello hello world wo", 'cause there's not enough room for the second world
} …Run Code Online (Sandbox Code Playgroud) 但我不知道如何在我的c程序中生成能够产生正确答案的状态嵌套...
我试图编译以下代码,但编译器不会这样做,因为"*对于结构体来说是非法的"是真的吗?
struct String {
int length;
int capacity;
unsigned check;
char ptr[0];
} String;
void main(){
char *s;
String *new_string = malloc(sizeof(String) + 10 + 1);
}
Run Code Online (Sandbox Code Playgroud) 我已经被指示通过在堆上创建一个String结构来编写模型strdup,它包含源的副本.我想我已成功编写了strdup,但我不确定我是否在堆上创建了一个Struct ...
typedef
struct String {
int length;
int capacity;
unsigned check;
char ptr[0];
} String;
char* modelstrdup(char* src){
int capacity =0, length=0, i = 0 ;
char *string;
while ( src[length] != '\0'){
length++;
}
capacity = length;
string = malloc(sizeof(String) + capacity + 1);
while ( i < length ){
string[i] = src[i];
i++;
}
string[i+1] = '\0';
return string;
}
Run Code Online (Sandbox Code Playgroud) 这个样本作业问题的答案是"1,000,000",但我不明白为什么:
以下代码的输出是什么?
int main(void) {
float k = 1;
while (k != k + 1) {
k = k + 1;
}
printf(“%g”, k); // %g means output a floating point variable in decimal
}
Run Code Online (Sandbox Code Playgroud)
如果程序无限期运行但不产生输出,请写INFINITE LOOP作为问题的答案.所有程序都编译运行.但是,它们可能包含或可能不包含严重错误.您应该假设int是四个字节.您应该假设float具有相当于六位小数的精度.你可以将你的答案四舍五入到最近的10的幂(例如,你可以说1000而不是2 10(即1024)).
我不明白为什么循环会终止.
在下面的代码中,我得到了Unreachable catch block for IOException.此异常不会从try语句体错误(下划线)与扔IOException在} catch (IOException e){我究竟做错了什么?
class driver {
public static void main(String[] args) {
// TODO Auto-generated method stub
Customer forrest = new Customer("Forrest Gump", 1,
"42 New Street, New York, New York");
Customer random = new Customer("Random Name", 2,
"44 New Street, New York, New York");
Customer customer[] = { null, forrest, random };
int whichOption = 1;
int id = 0;
char action = ' ';
char accSrc = …Run Code Online (Sandbox Code Playgroud) 我得到了无法封闭的类型测试实例.必须使用类型测试错误的封闭实例限定分配,Location ob1 = new Location(10.0, 20.0);我不知道为什么..
package pkg;
public class test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Location ob1 = new Location(10.0, 20.0);
Location ob2 = new Location(5.0, 30.0);
ob1.show();
ob2.show();
ob1 = ob1.plus(ob2);
ob1.show();
return;
}
public class Location // an ADT
{
private double longitude, latitude;
public Location(double lg, double lt) {
longitude = lg;
latitude = lt;
}
public void show() {
System.out.println(longitude + " " + latitude);
} …Run Code Online (Sandbox Code Playgroud)