![]() |
|
#1
|
|||
|
|||
Passing strings by referenceI am having a problem with a method that needs to divide a string in two. So it need a string as an argument and it have to return 2 strings. That means this 2 strings must be passed by reference (I hope the terminology is right since english is not my main language). Anyway, the code is working perfectly because I tested as a "main" function. And the method compile perfect too. But at execution time when the method is called it gives me a memory access violation error. Here's the method:
CPP / C++ / C Code:
I create three strings to call the method: CPP / C++ / C Code:
And the call of the method: CPP / C++ / C Code:
Thanks in advance. |
|
#2
|
|||
|
|||
Re: Passing strings by referenceI was writing a lengthy answer here again, but no. In short, your "array of pointer to char" -approach sucks. Ditch it. If your compiler didn't give any warnings or errors, ditch that compiler too. Then you might reconsider making some bound checking in your function.
Oh and access violation usually means something is not working perfectly eh? If something compiles, in my short experience it usually means the syntax might be OK, but the program still sucks. |
|
#3
|
|||
|
|||
Re: Passing strings by referenceHow can I have 2 strings by reference? I just need the method returns 2 strings. And you are right it sucks
|
|
#4
|
|||
|
|||
Re: Passing strings by referenceOkay after a good night's sleep here's a bit longer version.
1. Well about your terminology. It's not a method, it's just a function. You don't pass any strings to the function, you just pass the address. If you print the addresses in main() and your function, they are the same. The function doesn't return anything. You just modify the contents of the addresses passed to the function. 2. It's not working perfecly apparently is it. 3. You could've also given the main() and the complete program to test with. 4. CPP / C++ / C Code:
CPP / C++ / C Code:
5. The numbers between the brackets indicating the lengths of the arrays are useless. I didn't try it, but I guess the compiler shouldn't accept if, for example, you passed sizes as variables. CPP / C++ / C Code:
CPP / C++ / C Code:
6. 35 + 6 + 29 = 70. 6 + 25 + 29 = 60. So if your size arguments actually were if any use here, your function would think input is size 35, when in fact its size is 29. 7. The bound checking I mentioned. The compiler nor the program cares about or checks the bounds of your arrays. Your function will happily copy chars from input to comando as long as they are not equal to space or null character and as long as your operating system allows it to. 8. Your weird pointer array approach. Why? Did it tell you on the assignment or something to use that? 9. You actually don't create strings, just arrays. I guess it's a string when you terminate it with '\0'. But your pointer arrays are not strings, no matter how hard you null them. 10. My compiler doesn't seem to like assigning NULL to characters. I think you should use '\0'. Just 0 is okay too? 11. You might want to read a bit more about arrays and pointers. It seems to me that either you a) come from a different language doing stuff like it was done in that language or b) you just hastily looked over some pointer stuff and made this your first program. 12. Hope this helps. EDIT Quote:
|
|
#5
|
||||
|
||||
Re: Passing strings by referenceYou have a mismatch in declaration (sort of), and usage...
1. input is declared with [29], but you pass it as [35]? 2. the other two arrays should just be regular char arrays, and not char* arrays. (and pass them like you did with 'input', as arrays are passed by reference) Note that when you pass: CPP / C++ / C Code:
I'll let you ponder this for a bit, but if you still have trouble, post another reply. __________________
Use the force...read the source!! WYCIWYG -- what you code is what you get! |
|
#6
|
|||
|
|||
Re: Passing strings by referenceQuote:
A little clarification. The following defines a function whose parameter is a pointer to char: CPP / C++ / C Code:
The following defines a function whose parameter is a pointer to char: CPP / C++ / C Code:
The following defines a function whose parameter is a pointer to char (the constant in the brackets is ignored): CPP / C++ / C Code:
The following points apply equally to the three examples. The three functions have identical code. 1. When you call any of these functions, the argument is always a pointer to char. 2. Note that in C and C++, the name of an array is a constant pointer to char. The value of this pointer is the address of the first element of the array, so you can use the name of an array as the argument in the function call. The function does not know or care whether the argument was the name of an array or was a variable that had been declared a pointer to char. Or a dereferenced pointer to pointer to char or anything else. The argument is a pointer to char. Period. 3. There is no way (no way) that a function in C or C++ can know the size of the array unless you tell it. Either with a separate integer parameter or with a global variable (or any variable that is in the scope of the function). In particular, the last example does not (not) tell the function that the size of the array is 17. 4. Inside the function, the argument can be used with array notation or pointer notation. They are interchangeable. That does not (not) mean that arrays are the same as pointers. It's just notation. 5. This is the C++ board. Everything you posted is valid C (and, by the way, it is also valid C++). In C++ when people talk about strings, they usually mean objects of the std::string class. You don't have a string anywhere in the post. (In C there is no "string" data type.) In C, when we have functions that talk about "strings", like strcpy(), strcat(), printf("%s"), the arguments are pointers to char. A C-style "string" is a zero-terminated sequence of chars somewhere in memory. So an array of char can contain a "string", but an array of char is not a "string". 6. In C programs, all parameters are passed by value. You can not (not) pass an array to a function. You can pass a pointer whose value is the address of the first element of the array by just using the name of the array in the calling function. 7. Sometimes people say that this is passing the array "by reference", but that's not the same as in C++ where a "reference" is an alias for some variable. In C++ (but not in C) you can use "pass by reference" with any kind of variable. The mechanism in the code generated by a C++ compiler for arguments that are passed by reference is the same as that for passing a pointer, but the notation in the source code is different. Regards, Dave Footnote: CPP / C++ / C Code:
Code:
1. The value of the pointer may be different, but its value is unimportant. The main point is that the argument is a pointer to char. 2. The size of a pointer is not defined by the C standard. All compilers that I current use on 32-bit systems have the size of a pointer equal to 4. All compilers (all one of them) that I use on 64-bit systems have the size of a pointer equal to 8. This does not change the point that I am trying to make: in all cases, the argument is a pointer to char. |
|
#7
|
|||
|
|||
Re: Passing strings by referenceQuote:
I have been asked whether this statement is correct in light of the following from the original post: Quote:
Now upon first glance (and maybe even second or third glance) some people may think that the second and third arguments have something to do with C++ "call by reference," since that's the only place that most of us have seen '&' in a function call. Not so: In general, if xxx is some kind of pointer (any kind), the notation &*xxx is parsed right to left as follows: The value of *xxx is equal to the value of whatever it is that xxx is pointing to. The expression &yyy is the address of whatever yyy is Therefore &*xxx is the address of (the value of whatever the pointer xxx is pointing to). So, the value of xxx is equal to the value of &*xxx. To be a little more specific: comando in the original post is an array of pointers to char. I'll use a different name, and I will initialize the array just to have something definite to talk about: CPP / C++ / C Code:
y is an array of pointers to char. There are two elements; each is a pointer to char. I have initialized the array as follows: y[0] is a pointer to char. The value of y[0] is the address (somewhere in program memory) of the sequence of chars 'a', 'b', 'c', 'd', 0. An more succinct terminology is to say that the value of y[0] is the address of the string literal "abcd". Similarly, the value of y[1] is initialized to be the address of the string literal "efgh". y used by itself in a program is a pointer whose address is the first element of the array y. Since y is an array of pointers to char, we see that y to to a pointer to pointer to char. The value of y is the address of the first element in the array of pointers. Now, *y dereferences y, so the value of *y is a pointer to the string literal "abcd". Now if we take the address of *y we will have the address of the pointer to the string literal "abcd". This is equal to the address of the first element of the array. Bottom line: y is equal to &*y. (This holds for every data type, not just arrays of pointers to chars.) Therefore, the description in the original post has nothing to do with "reference" variables of reference arguments in the C++ meaning of the word "reference." It doesn't make sense (to me) to do it the way that the original poster tried (and I can't see any way that it would accomplish anything useful), but that wasn't the point of my post. Try the following C program: CPP / C++ / C Code:
Output from gcc on my Linux workstation: Code:
Call it test.c and compile it as C. Call it test.cpp and compile it as C++. Both are legal programs and give consistent outputs. So, no matter what the original functionality was intended to be, the function arguments are pointers. There aren't any strings, and nothing is "called by reference" in the C++ sense of the word. Period. Full stop. Regards, Dave Bottom line: the notation &*xxx in place of xxxis silly and some would say obfuscatory, but it is not incorrect or ambiguous from the point of view of C or C++ compilers. To hammer the point home (I hope), make the following changes: CPP / C++ / C Code:
names of arrays of pointers are pointers to pointers. Not "almost like" or "similar to" or "more-or-less the same as." From the compiler's point of view, they are identical. That doesn't mean that arrays are the same as pointers. It's just notation. |
|
#8
|
|||
|
|||
Re: Passing strings by referenceKeep up the good works.
|
Recent GIDBlog
Toyota - 2008 November Promotion by Nihal
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| gnu.linkonce undefined reference | newbie06 | C++ Forum | 4 | 13-Mar-2007 10:53 |
| passing a structure by reference | nic.nmd | C Programming Language | 1 | 18-Oct-2006 08:37 |
| Passing an object as a reference. What is wrong? | _Y_ | Java Forum | 1 | 29-Aug-2006 12:58 |
| G++ question... | TreyAU21 | C++ Forum | 10 | 24-Feb-2006 09:45 |
| Passing strings in functions..HELP please... | mgdpetter | C Programming Language | 2 | 24-May-2004 23:02 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The