SRL Archive - C/C++ Help and Tutorials

Char*, Char *, Char *var, Char var[] ... Me is confused...


Jason2gs

2008-04-05 18:44:58

Oh, and strings!

Yes. I'm quite confused about all the different char data types in C++.

I'm also trying to figure out (through trial-and-error, mostly) how to use strings and chars at the same time. Er… That doesn't really make sense, does it?

    string temp = "POST /ResultEJ.aspx HTTP/1.1\r\n";
temp += "Host: www.englishjapaneseonlinedictionary.com\r\n";
temp += "Content-Type: application/x-www-form-urlencoded\r\n";
temp += "Content-Length: "; temp.append((const char*)strlen(argv[0])); temp += "\r\n\r\n";
temp += "search="; temp.append(argv[0]); temp += "\r\n";

Line four messes up, and spits:

Segmentation fault (core dumped)


Out into the terminal.

I'm sure I'm messing something up here. I just don't know what.

Could someone lend a hand?

And regarding the subject line…

What do all the asterisks in different places do? And why can't you initialize a char *var like this?:

char *msg = "Hewwo!" + "Goodbye!";

Thanks for reading. Please see what you can do,

-Mike

R0b0t1

2008-04-05 22:45:37

strln(argv[0])….

Trying to get the string length of a character?


Or… it could be you are trying to convert it into a const. Just try (string) or (char*)


And, about the asterisks:


char* var {is equal to} char * var {is equal to} char *var

And the reason you can't initialize a char* like that is because, well, you need to give it a length.


char *var[15];

var = strcpy(var, "Hewwo!" + " Goodbye!";

Jason2gs

2008-04-05 23:52:20

They're all the same? Well that clears up a lot :)

And I am trying to get the length of the search query.

With a POST request, you have to provide the length of the post variables. (Eg., var1=hello would be 10. Likewise, var1=hello&var2=goodbye would be 23. You have to include everything.)

Which just reminds me, that won't work right. You have to include "search=" in with the number given.

R0b0t1

2008-04-06 15:57:27

Wait.. Why can't you just do this on the browser side? Handling the search request… Or are you sending it from the application?

R0b0t1

2008-04-06 15:58:49

Wait.. Why can't you just do this on the browser side? Handling the search request… Or are you sending it from the application?

Jason2gs

2008-04-06 19:46:00

Sending it from the application.

R0b0t1

2008-04-06 21:06:23

Ah, I see.


I'm pretty sure that there are libraries for this sort of thing…

Jason2gs

2008-04-06 21:39:16

*Holds up Vampire cross with some garlic attached*

Libraries… Me no like libraries!

Yakman

2008-04-07 04:49:11

From: Jason2gs
Libraries… Me no like libraries!


How to steal code

R0b0t1

2008-04-07 18:26:53

Hey - that sounds like communism! :p

Jason2gs

2008-04-07 21:28:35

From: Yakman
How to steal code


*Pushes glasses up on nose*

Haha… Cool :)

Hm… I bet I can borrow some of the source code from the POST script…

boberman

2008-04-17 10:56:13

humm, sounds like you need a stringstream… works just like a cout (or cin if you want it to) statement only it stores whatever you want. Foe example.


#include <ssteam>
using namespace std;

int main()
{
ostringstream in;
char test[] = "test";
int number = 7;
double pi = 3.14;
ostringstream << test << " number: " << number << " Pi: " << pi:
cout << ostringstream.str() << endl;
}

Output should be
test number: 7 Pi: 3.14
string streams are nice, love them.