小编use*_*466的帖子

在C中将字母转换为数字

我正在尝试编写一个将字母转换为数字的代码.例如,A ==> 0 B ==> 1 C ==> 2,依此类推.我想写下26条陈述.我想知道是否有更好的方法来做到这一点......

谢谢!

c

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

为什么我们需要转换malloc返回的内容?

    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的结构

c heap malloc

6
推荐指数
3
解决办法
6752
查看次数

如何编写Windows应用程序?

我是编程新手,我想写一个Windows应用程序.到目前为止,我已经阅读了24小时内自学C教学,我应该做什么(阅读教程)旁边完成我的目标?谢谢

c api winapi

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

如果我尝试访问malloc()区域以外的内存会发生什么?

我已经分配了一些内存,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 malloc memory-management

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

如何计算两个给定日期之间的天数?(闰年障碍)

  • 任何可被400整除的年份都是闰年(例如,2000年是闰年).
  • 任何可被100整除的其他年份不是闰年(例如,1700,1800和1900不是闰年).
  • 任何可被4整除的其他年份是闰年(例如,1996年和2004年是闰年).

但我不知道如何在我的c程序中生成能够产生正确答案的状态嵌套...

c algorithm

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

*结构是非法的?

我试图编译以下代码,但编译器不会这样做,因为"*对于结构体来说是非法的"是真的吗?

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)

c struct pointers

4
推荐指数
3
解决办法
1499
查看次数

在堆上创建结构?

我已经被指示通过在堆上创建一个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)

c heap

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

为什么这个浮点循环终止于1,000,000?

这个样本作业问题的答案是"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)).

我不明白为什么循环会终止.

c floating-point

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

try-catch不会捕获IOException

在下面的代码中,我得到了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)

java try-catch ioexception

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

不能访问封闭的类型测试实例.必须在简单的测试程序上使用类型为测试错误的封闭实例来限定分配

我得到了无法封闭的类型测试实例.必须使用类型测试错误的封闭实例限定分配,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)

java

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