我想我在C命令行参数上有点生疏了。我看了我的一些旧代码,但是不管这个版本是什么,都会出现分段错误。
运行的方式是./foo -n num (其中num是用户在命令行中输入的数字)。
但不知怎么的,它不起作用了。我哪里出问题了?
编辑:当我尝试访问atoi(optarg)时,它是分段错误,这是atoi(0x0),它是分段错误。
int main(int argc, char *argv[])
{
int c;
int maximum_n = max_n(); /* Stores the maximum value in the sequence a long can hold *
希望这是一个非常简单的问题。下面是我的C pgm (test.c)。
#include <stdio.h>
//#include <stdlib.h>
int main (int argc, char *argv[]) {
int intValue = atoi("1");
double doubleValue = atof("2");
fprintf(stdout,"The intValue is %d and the doubleValue is %g\n", intValue, double
我正在自学C语言的结构,在编译这段代码时遇到了困难:
#include <stdio.h>
#include <stdlib.h>
struct Date {
int Month;
int Day;
int Year;
};
void AddDecade(struct Date);
int main(int argc, char *argv[]) {
struct Date BDay;
char buffer[50];
printf("What month were you born? ");
我有一个map[string]string,我需要测试一些键是否存在,如果存在,就将一些值转换为整数。例如:
m := map[string]string{"a": "b", "c": "d", "e": "f"}
if v1, ok := m["a"]; ok {
if v2, ok := m["c"]; ok {
if i1, err := strconv.Atoi(v1); err != nil {
if i2, err := str
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main(){
int n;
int a,b,ans[10000];
char *c,*d,*e;
int i = 0;
c = (char*)(malloc(20 * sizeof(char)));
d = (char*)(malloc(20 * sizeof(char)));
scanf("%d",&n);
while(i < n){
我正在编写一个程序,该程序接收来自用户的输入,将其存储为一个数字字符串(假设没有错误的输入),并将这个字符字符串/数组转换回整数。我意识到我本来可以将输入存储为整数,但我正在尝试一些转换方法。
目前,我的错误涉及函数atoi(),它具有“冲突类型”接收的消息。
#include <stdio.h>
#include <stdlib.h>
int atoi(char s[])
{
int i, n;
n = 0;
for (i = 0; s[i] >= '0' && s[i] <= '9
我正在尝试使用atoi函数来实现从string到int的转换。问题是我有一个字符串数组,其中包含整数和字符串值。
据我所知,为了从中获取错误代码,该函数必须返回0:
string s = "ssss";
int i = atoi(s.c_str())
if (i == 0)
cout<<"error"<<endl;
end;
如果我的字符串值为0,我应该如何继续?
另一个问题是这个字符串:string s = "001_01_01_041_00.png"。atoi函数返回值1。它不应该返回0吗。为什么它返回1
我正试图从atoi()中找出函数stdlib.h。根据K&R,如下所示:
int atoi(char s[]) {
int n, i;
n = 0;
for (i = 0; s[i] >= '0' && s[i] <= '9'; ++i)
n = 10 * n + (s[i] - '0');
return n;
}
据我所知,来自atoi()的stdlib.h函数应该将任何字符的字符串作为输入,并且实际上只输出数字,如下所示:
代码1:
#include <std
atoi()给我这个错误:
error C2664: 'atoi' : cannot convert parameter 1 from 'char' to 'const char *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
从这一行开始: int pid = atoi( token.at(0) );其中token是一个向量
我怎么才能绕过这个问题呢?
我创建了一个解析字符串的函数,该函数看起来像"NAME:Sebastion \ AGE:4平行日期:8/2/2005“,我试图得到"2005”年。我编写了一个函数如下。当我叫"atoi“时,线路上会出现分段故障。那个英勇的人告诉我
Invalid read of size 1
==25142== at 0x5171167: ____strtol_l_internal (strtol_l.c:298)
==25142== by 0x516DB6F: atoi (atoi.c:27)
==25142== by
我编写了一个C程序,它使用两个不同的算法将表示数字值的字符串常量转换为整数值。由于某些原因,第一个算法atoi()不能在大值上正确执行,而第二个算法atoi_imp()工作得很好。这是优化问题还是其他错误?问题是,第一个函数使程序的进程以错误结束。
#include <stdio.h>
#include <string.h>
unsigned long long int atoi(const char[]);
unsigned long long int atoi_imp(const char[]);
int main(void) {
printf("
我编写了这段代码,以检查atoi() function.Please评审实现中的所有边缘情况。
//Program to print an atoi().
public class Atoi{
public static int atoiFunc(String str,char[] c,int l){
//If the length is 0,the function returns 0;
if(l==0){
return 0;
}
//variable to store the result
int res = 0
我真的不明白为什么我会犯这个错误。
architect.c: In function ‘main’:
architect.c:91:20: error: incompatible types when assigning to type ‘t_result’ from type ‘int’
architect.c:93:20: error: incompatible types when assigning to type ‘t_result’ from type ‘int’
architect.c:95:20: error: incompatible types when assigning
我一直在尝试编写一个非常基本的函数来确定两个日期之间的差异(以天为单位)。我是C++的新手,所以你可以想象我对我的工作很满意。这个函数是一个大得多的代码的一部分,所以我忽略了我的包含,但是函数在编译时没有警告。
只是寻求一些建设性的批评,一个合理的初学者可以接受。
我要补充的是,这个函数将两个日期作为英式格式DD/MM/YYYY的字符串。
int dateDiffs(std::string date1, std::string date2){
int startYear, endYear;
int startMonth, endMonth;
int startDay,
这绝对是个新手问题。
Go中的一些函数返回多个值(通常是值和错误)。我正在写一个函数,它返回其中一个函数的返回值,即使把值放在变量上并只返回第一个变量是很容易的,我怀疑我是否能在没有额外变量的情况下只用一行就能做到同样的事情。这是C、Java、C#、Ruby等其他语言中经常用到的
func someFunc (param string) int {
// do something with the string, not very important
return strconv.Atoi(param)
}
我知道这很管用
func someFunc (param string)