I've been trying this for about an hour and looked at other places online but still was not able to accomplish what I'm trying to do. This should probably be something simple but I have not coded in C in quite awhile so I may be missing something. Anyway I want to use strcmp to compare something in a variable called char *args and a literal like "qw".
I am in main and pass a function this args variable and when I get it back I want to compare like this: Maybe I am just completely doing this in a dumb way.
char *args;
char **svdArray;
int keepTrack = 0;
int beforeTrack = 0;
svdArray = malloc(5*sizeof(char*));
while (1)
{
if(!(strcmp(args[0], "r")) && (!strcmp(args[0], "rr")))
{
svdArray[keepTrack] = args[0];
keepTrack++;
}
All I want to happen is if I args[0] has something in it besides rr or r I want to execute the code inside the if statement. However as of now the flow just never enters it and I don't know why.
Any help would greatly be appreciated!!!
strcmp() compares both strings in whole.
To only test for parts use strstr() like for example so:
if (strstr(args, "r")) || strstr(args, "rr")))
{
svdArray[keepTrack] = args;
keepTrack++;
}
You need to pass a char* to strcmp, like args.
Here is the prototype for strcmp from here:
int strcmp ( const char * str1, const char * str2 );
So since args is a char* and "r" will give you a char* you should try:
/* since you're learning, print out what this thing does */
#include <stdio.h>
printf("Output: %d\n",strcmp(args,"r"));
Related
I'm studying C in order to start doing some fun low-level code stuff, and I've stumbled into a scenario which I can't wrap my head around, and I'm sure it's because I don't have a lot of experience with it.
Currently my code is very simple: it takes some arguments, and gets the first parameter passed to main, and stores it as a path string. The first question that came to mind, was whether it would be correct to store the main params as char *args[] or char **args, and I decided to go with char **args since according to this question there could be some scenarios where the first would not be accessible, and I just wanted to make a code that would be as complete as possible, and learn the whys on the process.
Here's is the code:
int main(int argc, char **args) {
if (args[1] == NULL) return 1;
// Get path of input file
char *path = &*args[1];
fputs(path, stdout);
return 0;
}
Given the code above, what would be a better way of fetching the value stored in *args[1]? It seems very cryptic when I look at it, and it took me a while to get to it as well.
My understanding is that char **args, is a pointer, to an array of pointers. Thus, if I'm to store a string or any other value for later use in one of the indexes of args, I would have to assign a new pointer to a memory location (*path), and assign the value of the given index to it (&*args[i]). Am I over complicating things? Or is this thought process correct?
For starters these two function declarations
int main(int argc, char **args)
and
int main(int argc, char *args[])
are fully equivalent because the compiler adjusts the parameter that has an array type to pointer to the array element type.
In the initializer expression of this declaration
char *path = &*args[1];
applying the two operators & and * sequentially is redundant. So you may just write
char *path = args[1];
Also in general instead of the condition in the if statement
if ( args[1] == NULL) return 1;
it will be more safer to write
if ( argc < 2 ) return 1;
You can simply write:
char *path = args[1];
& and * operators are inverses of each other, so &* or *& can simply be removed from an expression.
I am trying to work with only the first character of a string, that is within argv[]. I was thinking something like argv[1[2]], but that doesn't work or make much sense.
As with a lot of things in C, you de-reference the pointer, and the easiest way is by treating it as an array:
char** arguments = argv;
char* first_argument = argv[0];
char first_letter = argv[0][0];
This is also equivalent to:
char first_letter = first_argument[0];
And you can go in reverse by assigning the address of something to a pointer:
char* argument = &first_letter;
Your attempt is this:
argv[1[2]]
Which implies there's some value like this:
int i = 1[2];
argv[i];
Where 1[2] is not something C can deal with.
I am trying to compare szFileName1 and szFileName2 , if they are not same, then I am renaming it, but when I am trying to concatenate using snprintf it's giving segmentation fault.what mistake am I doing here?
typedef struct{
char filePath[100];
} filectx;
void filename1(filectx *ctx, const char ** szFileName1){
*szFileName1 = ctx->filepath;
}
void filename2(const char ** szFileName2){
char buf[20] = "/usr/bin/abb.txt";
snprintf(szFileName2, sizeof(szFileName2), "%s%s", szFileName2, buf);
}
int main(){
const char* szFileName1 = NULL;
const char *szFileName2 = malloc(100);
filectx ctx;
ctx.filePath = "/usr/bin/abc.txt";
filename1(&ctx, &szFileName1);
filename2(&szFileName2);
if(strcmp(szFileName1, szFileName2) != 0){
const char szFilePath1[200] = "/local/";
const char szFilePath2[200] = "/local/";
snprintf(szFilePath1, sizeof(szFilePath1), "%s%s", szFilePath1, szFileName1);
snprintf(szFilePath2, sizeof(szFilePath2), "%s%s", szFilePath2, szFileName2);
int ret = rename(szFilePath1, szFilePath2);
}
free(szFileName2);
return 0;
}
I think the problem here is with the arguments you pass to snprintf().
snprintf() expects an argument of type string ("char *"), but not "char **".
Here you are passing a pointer instead of the actual string. So when it tries to access the address it gives segmentation fault.
Change the parameters in the functions filename1() and filename2() to "char *" type and see. It should work.
Hope this helps.
Kranthi
With
const char szFilePath1[200] = "/local/";
const char szFilePath2[200] = "/local/";
as well as others as your function arguments, you declare those variables const. You then try to write to them with snprintf. Don't make these const.
You also can't reuse a variable as source and destination in snprintf.
I'm surprised that the compiler allowed you to compile this.
Although snprintf() doesn't work in your case, why don't you use strcat() or strncat()?
Instead of
snprintf(szFilePath1, sizeof(szFilePath1), "%s%s", szFilePath1, szFileName1);
you can write
strncat(szFilePath1, szFileName1, strlen(szFilePath1));
And by the way:
WHY did you write
sizeof(szFilePath1)
?
So, points to improve
In the very beginning:
*szFileName1 = ctx->filepath;
Not a nice thing to do. It's better to use strcpy()/strncpy(). And passing char ** argument also looks pretty strange.
Stated above. Usage of snprintf's
I'm passing values beteween childs and need to store some values to later use.
the definitions and use in functions
char fouts[MAX_SIZE][10];
the function where i give the array the values:
void connect(char *nodo, char *out[], int nouts) {
(...)
for(i=0;i<nouts;i++) {
fouts[fnum][i] = out[i];
}
(...)
and the function where i'm trying to use them:
void disconnect(char *nodo, char *remover){
char *outs[10];
nouts = fnouts[getfnum];
int m =0;
for(i=0;i<nouts;i++) {
if(strcmp(fouts[getfnum][i],nodo) != 0) { outs[m] = fouts[getfnum][i]; m++ ; }
}
no matter what i did to try to fixm everytime it tries to execute this last for, it gives a segmentation fault.
have tried somethings (read fouts[getfnum][0] for example directly and gives a segmentaton fault, but fouts[getfnum] gives "trash")
check the value after it been atributed fouts[fnum][i] = out[i]; here and it checks out, so i guess that part is ok).
don't know if its something obvious or not, but any help?
You are mixing char and char*.
fouts[fnum][i] is a char
and
out[i] is a char pointer
So in this line
fouts[fnum][i] = out[i];
you assign a char pointer to a char which is illegal.
And in this line
if(strcmp(fouts[getfnum][i],nodo) != 0)
you pass a char (i.e. fouts[getfnum][i]) to strcmp.
That is not legal as strcmp expects a char*
From the posted code, it is hard to tell how to fix the problems. Maybe you just need:
char* fouts[MAX_SIZE][10];
Hi I really can't get my head around this. I'm basically trying to return a char array from a function by passing the output array in as a parameter. Here is what I have so far:
The function:
int GetComputerName(char *name, char *ip_address){
*name = "testing";
return 0;
}
And calling it:
char comp_name[50];
GetComputerName(&comp_name, serverIP);
printf("\n\n name: %s\n\n", comp_name);
I have tried switching and swapping the * and & to see what will work and have read up on pointers and stuff yet what I think should be happening an what actually does happen is two very different things and now I think I have confused myself more than when I started!! lol
Can someone please help me out and explain what the correct way of doing this is?!
Thanks in advance =)
This line:
*name = "testing";
is invalid, as you assign the pointer to "testing" into a char pointed by name. You need to copy the data into the buffer. It should be:
int GetComputerName(char *name, char *ip_address){
strcpy(name,"testing");
return 0;
}
or even better (to avoid overflows):
int GetComputerName(char *name, size_t buff_len, char *ip_address){
strncpy(name,"testing", buff_len);
name[buff_len - 1] = '\0';
return 0;
}
And call it:
GetComputerName(comp_name, sizeof(comp_name), serverIP);