So it's been a few years since I've done any significant programming in C or C++ so my syntax is a bit rusty.
I'm going to be adding some system calls to a linux kernel, some of which will accept a character pointer (that points to an array of characters) as an argument.
My old C book sucks and barely covers working with character arrays and it's been many, many semesters since I had my C course notes bookmarked. All I remember was having issues with when I needed a * and & so I'm hoping for some help with syntax to do the following:
1) The prototype that goes in the .h file.
2) The declaration that goes into the .c file I'll be writing out the function in.
Google only seems to turn up stuff that doesn't involve passing this stuff to functions.
On a side note, if anyone can think of a good replacement for my old, sucky C book, feel free to name them.
I found that tilting it doesn't work very well, and once I started jerking it, I got much better results.
Steam Profile
3DS: 3454-0268-5595 Battle.net: SteelAngel#1772
Posts
So if you had:
void someFunction(char * arrayChar);
I *think* (correct me if I'm wrong, it's been a long time since I worked with a language that uses pointers directly) you can do all of the pointer math on that "arrayChar" variable passed in to get to the subsequent members:
arrayChar[5] (6th position in the array)
etc...
IMPORTANT NOTE: Someone correct me if I'm wrong. It's been a long, long time. I'm giving from the best that I can remember, so I apologize in advance if it's incorrect. But give it a shot, try it with a small test function and see if it works as advertised. Then report back here.
void methodName(char* argPointer)
Then to call it:
char* newString = new char[50]; //50-character long string, null terminator included
newString = "this is a test";
methodName(newString);
Ah, good, I wasn't crazy then. I posted it thinking it was right, but I was having some severe self-doubt about it.
Don't mean to sound an*l retentive, but you will need a semi-colon behind that. Else the compiler will give you an error, most likely on the next non-blank line.
Also, I'm afraid you've written C++ (new is a C++ keyword). =/ As far as I'm aware, the Linux kernel is written entirely in C, with no C++, so it may be best to avoid C++ syntax. String assignment with the equals sign is not legal either (I can't remember exactly, but at the very least you're passing a const char * to a char *, thus attempting to discard the const).
Anyway, here's an example in C:
test.h:
test.c:
Depends on if it's the definition or the declaration. :P
So this is valid:
char test = 'a';
char* testptr = &test;
Now test is 'a', and testptr is a pointer to it.
Makes sense?
I have no issue when I'm referencing/dereferencing something in the same scope as declaring it, it's when it gets passed in from another source that it starts to get confusing. Like say:
void SomeFunction (char *testptr) {
do stuff with testptr;
}
That's when I start to get confused.
Steam Profile
3DS: 3454-0268-5595 Battle.net: SteelAngel#1772
It depends on what testptr is.
If testptr is a character array (i.e., a string), then you pass it it without taking the address, since it already is a pointer. For instance:
However, there are situations where you may want to pass something as a pointer that isn't a pointer to start with. A common example in C is for functions that return error codes or some such, and need to modify the values passed in as arguments. For instance: Will print out "5" to the console, since the value of "test" was not modified outside the scope of foo. However, if we change the example to this: This will print "4". Why? We passed a pointer to test to foo. foo then dereferenced the pointer using the * operator, i.e., it sets the value of the thing pointed to by a to 4. Since the thing pointed to by a is test, the value of test in memory was modified.
This all gets really fun when you need to modify the value of strings passed to a function, in which case you get into the land of double pointers.
Um, does this make any sense?
It is true Doc gave a C++ example, but I'm not entirely sure what this has to do with the Linux kernel. There are plenty of reasons to write in C or C++, but the language the kernel is in isn't one of them, unless there's a discussion about what language is appropriate for systems programming that I missed.
Yeah, that's true. Too much time with Python has caused me to start using other languages in invalid ways, heh.
Not entirely. It is legal to initialize a string to a constant string (like in my example), but that's the only place.
In the original post Steel Angel mentioned he was going to be adding some system calls to the Linux kernel.
Are you able to give us an example of what you'd like to do with the pointer? I'm sure many of us can show you how to do it and explain why - maybe that'll help better than us giving you advice on general pointer usage?
While true, it is a bit dangerous since the pointer is to a const string. Attempts to modify it (not unreasonable when someone gives you a char *) will cause the programme to segfault/generate an access violation. For example, this programme crashes:
What you may have meant may be:
where pString is now a string array initialised with the string "hello" instead of a char * to a const string.
To be fair, that would've been legal in C++ had you used the std::string class. =P
Nothing horribly complex. I'm basically taking a character array and an integer n, then copying the first n values of the passed in array into one I'll dynamically allocate. The allocated array is going to be declared using extern so I don't have to return a pointer to it (I need to return an numerical value anyway just so I can pass back an error code if stuff goes wrong)
So I expect to have something that looks like this:
So I'll need to know the syntax for dealing with the array being passed in by reference and what, if any, use of * I'll need during the strcpy.
Steam Profile
3DS: 3454-0268-5595 Battle.net: SteelAngel#1772
So in that case, you'll want something that goes vaguely like:
You typically wouldn't use strcpy in this application as it copies data until it hits a null terminator, irrespective of length. Could be the cause of a buffer overflow.
You would use either:
1.) memcpy (does not assume that the source data is an ASCII string, and will copy exactly nLength bytes)
or
2.) strncpy (assumes that the source data is a string, but has an option for the maximum number of characters to copy - it can always copy less if the string is less than the specified maximum number of characters)
depending on application.