strcmp

strcmp with pointers not working in C

那年仲夏 提交于 2019-11-29 13:53:01
Why this code isn't working. Just trying to check if the user input is the same as a password char *pass; printf("Write the password: "); scanf("%s", pass); // Because is a pointer the & is out ? if( strcmp( pass , "acopio") == 0) You've not actually allocated any space to put data. Defining a pointer just defines a variable that can hold the address of a block of data, it doesn't allocate the block. You have a couple of options, allocate dynamic memory off the heap to write into and make the pointer point to it. Or use statically allocated memory on the stack and pass the address of it to

What's wrong with strcmp?

耗尽温柔 提交于 2019-11-29 06:40:18
In the responses to the question Reading In A String and comparing it C , more than one person discouraged the use of strcmp() , saying things like I also strongly, strongly advise you to get used to using strncmp() now, ... to avoid many problems down the road. or (in Why does my string comparison fail? ) Make certain you use strncmp and not strcmp. strcmp is profoundly unsafe. What problems are they alluding to? The reason scanf() with string specifiers and gets() are strongly discouraged is because they almost inevitably lead to buffer overflow vulnerabilities. However, it's not possible to

When will strcmp not return -1, 0 or 1?

百般思念 提交于 2019-11-28 14:08:26
From the man page: The strcmp() and strncmp() functions return an integer less than, equal to, or greater than zero if s1 (or the first n bytes thereof) is found, respectively, to be less than, to match, or be greater than s2. Example code in C (prints -15 on my machine, swapping test1 and test2 inverts the value): #include <stdio.h> #include <string.h> int main() { char* test1 = "hello"; char* test2 = "world"; printf("%d\n", strcmp(test1, test2)); } I found this code (taken from this question ) that relies on the values of strcmp being something other than -1, 0 and 1 (it uses the return

代码审计-strcmp比较字符串

不打扰是莪最后的温柔 提交于 2019-11-28 11:34:50
<?php $flag = "flag{xxxxx}"; if (isset($_GET['a'])) { if (strcmp($_GET['a'], $flag) == 0) //如果 str1 小于 str2 返回 < 0; 如果 str1大于 str2返回 > 0;如果两者相等,返回 0。 //比较两个字符串(区分大小写) die('Flag: '.$flag); else print 'No'; } ?> strcmp(str1,str2) 如果 str1 小于 str2 返回 < 0; 如果 str1大于 str2返回 > 0;如果两者相等,返回 0。 比较两个字符串(区分大小写) 如果我们传入非字符串类型的数据的时候(数组),这个函数将发生错误,并且返回数值 0 在5.3之前的php中,显示了报错的警告信息后,返回数值0 我们可以构造payload http://123.206.87.240:9009/6.php?a[]=1 得到flag Flag: flag{bugku_dmsj_912k} 来源: https://www.cnblogs.com/gaonuoqi/p/11406736.html

MATLAB: Error using load Argument must contain a character vector.

谁说我不能喝 提交于 2019-11-28 10:33:30
MATLAB 在加载数据时报错: Error using load Argument must contain a character vector. Error in MATLAB_file (line 30) load([data_root filename1]); 错误代码位置: index = find ( strcmp ( { data_all . name } , filename1 ) ) ; 错误原因: MATLAB中strcmp用来比较字符串,这里参考 matlab strcmp ,对于MATLAB R2016b版本,可分为以下几种情况: (1)比较两个字符向量 (2)在cell中查找文本 (3)比较两个字符向量cell 我的情况属于第二种,前者 {data_all.name} 已经是一个cell元胞数组,目的是在元胞数组中查找第二项字符串的位置,即索引,但是程序中的 filename1 直接取自 data(i,1) ,而 data 本身也是cell元胞数组,因此需将 filename1 转换成character格式,即在之前加一句: filename1 = char ( data ( i , 1 ) ) ; 程序运行便不再报错。 来源: https://blog.csdn.net/S20144144/article/details/100053377

Implementation of strcmp

余生长醉 提交于 2019-11-28 08:38:44
I tried to implement strcmp : int strCmp(char string1[], char string2[] ) { int i=0,flag=0; while(flag==0) { if (string1[i]>string2[i]) { flag=1; } else if (string1[i]<string2[i]) { flag=-1; } else { i++; } } return flag; } but I'm stuck with the case that the user will input the same strings, because the function works with 1 and -1, but it's doesn't return 0. Can anyone help? And please without pointers! You seem to want to avoid pointer arithmetics which is a pity since that makes the solution shorter, but your problem is just that you scan beyond the end of the strings. Adding an explicit

strcmp with pointers not working in C

江枫思渺然 提交于 2019-11-28 08:02:31
问题 Why this code isn't working. Just trying to check if the user input is the same as a password char *pass; printf("Write the password: "); scanf("%s", pass); // Because is a pointer the & is out ? if( strcmp( pass , "acopio") == 0) 回答1: You've not actually allocated any space to put data. Defining a pointer just defines a variable that can hold the address of a block of data, it doesn't allocate the block. You have a couple of options, allocate dynamic memory off the heap to write into and

strcmp() return different values for same string comparisons

一曲冷凌霜 提交于 2019-11-28 07:30:23
问题 char s1[] = "0"; char s2[] = "9"; printf("%d\n", strcmp(s1, s2)); // Prints -9 printf("%d\n", strcmp("0", "9")); // Prints -1 Why do strcmp returns different values when it receives the same parameters ? Those values are still legal since strcmp's man page says that the return value of strcmp can be less, greater or equal than 0, but I don't understand why they are different in this example. 回答1: I assume you are using GCC when compiling this, I tried it on 4.8.4. The trick here is that GCC

(六)文件管理

拈花ヽ惹草 提交于 2019-11-28 02:12:05
文件管理的试题比较多,主要就是模拟操作系统中的 建立文件、打开文件、读文件、写文件、、关闭文件、 、删除文件、、建立目录、、 显示目录内容、显示文件内容、、改变文件属性等操作。大家可以参考书本253页的上机指导。 北大2001年试题: 建立一个树型文件目录 假设程序启动运行后在根目录下且根目录为空。 实习检查: 1、运行程序,由检查教师给出文件名,该文件中存有相应的若干命令。(程序应做提示,界面友好)。 2、要求实现两个命令: mkdir 目录名(目录已存在,应给出错误信息。) cd 目录名(目录不存在,应给出错误信息。) 3、你所编制的程序应读入文件,并执行其中的每一条命令。 4、在屏幕上显示文件目录的结构。(界面自己设计,但要清晰明了。) 2002年北京大学的试题: 操作系统上机考试题 题目:模拟文件系统 要求:模拟一个文件系统,包括目录文件,普通文件,并实现对它们的一些 基本操作。 假定每个目录文件最多只能占用一个块;一个目录项包括文件名(下一级目录 名),文件类型,文件长度,指向文件内容(下一级目录)的指针内容。普通文件可以 只用目录项(FCB)代表。(详细的数据结构见后面的说明) 程序功能方面的要求: 需要实现一个命令行操作界面,包含如下命令: 1 改变目录 格式:CD〈目录名〉 功能:工作目录转移到指定的目录下,只要求完成改变到当前目录的某一个子目录 下的功能

What's wrong with strcmp?

▼魔方 西西 提交于 2019-11-28 00:29:50
问题 In the responses to the question Reading In A String and comparing it C , more than one person discouraged the use of strcmp() , saying things like I also strongly, strongly advise you to get used to using strncmp() now, ... to avoid many problems down the road. or (in Why does my string comparison fail? ) Make certain you use strncmp and not strcmp. strcmp is profoundly unsafe. What problems are they alluding to? The reason scanf() with string specifiers and gets() are strongly discouraged