Comparing a char array

Presume i want to compare the argv[2]

What would be the best metod?
strcmp or others?
i'm currently using this:

1
2
3
std::string message = argv[1]
if(message == "Hello")
//dosomething 


But i think that is a waste of space, since declaring a new variable.

Oh and i'm wondering what is this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
typedef void(*mything)(int a);
struct MyStruct
{
mything mythingTwo;
}

void MyThingBig()
{
 //something
}

int main()
{

myThingTwo some;
some.mythingTwo = MyThingBig;
}

//What i'm i doing D: ?? 


and what's the point of this

1
2
int **apointer
// why it has 2 ** 
Last edited on
If these char arrays are null-terminated C-style strings, then you can use the standard C library function strcmp().
And that's the best way/memory free?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
typedef void(*mything)(int a);

struct MyStruct
{
mything mythingTwo;
}

void MyThingBig()
{
MyStruct tempstruct; //Declare a temporary struct to set values.
tempstruct.mythingTwo = NULL; //Set it equal to some value. I just set this pointer to null.
return tempstruct;
 //something
}

int main()
{
/*In order to declare a variable of the struct type you have above, you will need to
use the struct name as the datatype, 
then the name of the variable you want to declare. 
*/ 
MyStruct some; 
/* To call the function don't forget the parenthesis! 
Also, you don't need to reference the  
variable in your struct (some.mythingTwo)
if you're using a function to set the values (which I assumed you were doing). 
Just return the temporary struct to the caller.
*/
some = MyThingBig(); 
}


I hope that helped. I kind of assumed what you were trying to get at.

1
2
3
4
5
6
int** apointer;
/*This has two asterisks because it is a pointer(a variable that holds a memory address) that points to another pointer. 
You can have even more than that the farther you go. 
If you have a pointer that points to a pointer that points to a pointer, you can get something that looks like int*** apointer; 
These are used more when you get into dynamically allocated multidimensional arrays.
*/
Last edited on
Also, I don't think you can compare strings like that. I think you need to use strcmp(); Technically a string is an array of characters. So you're comparing a string literal to a pointer.
I saw people doing what i said.
Like in libircclient
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
typedef struct
{
  irc_event_callback_t         event_connect;
  irc_event_callback_t         event_nick;
  irc_event_callback_t         event_quit;
 //ecc
}

//and then addin to a value a funcion, withouth the parenthesis like this:

void myfunction(values value, values2 value, more ecc)
{
 //Lalala
}

irc_callbacks_t something;
something.event_connect = myfunction;

What's the point of typedef void(*mything)(int a); 

Thank's in advance :D
Also, I don't think you can compare strings like that
std::string can be compared with c-string directly.

What would be the best metod?
Whichever is clearer.
either yours or std::strcmp(argv[1]. "Hello");
But i think that is a waste of space, since declaring a new variable.
Premature optimization. Unless you on some embedded system with 32k of memory you should not worry about it. Also you can wrap it in separate block to make it auto-destruct.

typedef void(*mything)(int a);This code declares type alias mything as pointer to function taking integer and returning void. Is used because it is way better to write
1
2
3
4
5
6
7
irc_event_callback_t     event_connect;
irc_event_callback_t     event_nick;
irc_event_callback_t     event_quit;
//instead of 
void(*event_connect)(char*)
void(*event_nick)(char*)
void(*event_quit)(char*)


int **apointer is a ponter to pointer to int. Usually used to create two-dimensional arrays.
But why i don't need to use the parenthesis?
But why i don't need to use the parenthesis?
Where?
Here
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
typedef struct
{
  irc_event_callback_t         event_connect;
  irc_event_callback_t         event_nick;
  irc_event_callback_t         event_quit;
 //ecc
}

//and then addin to a value a funcion, withouth the parenthesis like this:

void myfunction(values value, values2 value, more ecc)
{
 //Lalala
}

irc_callbacks_t something;
something.event_connect = myfunction; //here 

line 17 - you need the parenthesis to call myfunction and include whatever parameters you defined the function to have

Edit: and it can't be a void function if you're expecting it to return something to something.event_connect


nm - I didn't catch that something.event_connect was a pointer.
Last edited on
You do not need parentesis because you do not calling function. You are assigning it address to something.event_connect pointer which you can use to call tat function later.
Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void foo()
{
    std::cout << "foo\n";
}

void bar()
{
    std::cout << "bar\n";
}

int main()
{
    using fptr = void(*)();
    fptr x = foo;
    x(); //Prints "foo"
    x = bar;
    x(); // prints "bar"
}
Topic archived. No new replies allowed.