![]() |
| | #1 |
| Registered User Join Date: Nov 2009
Posts: 1
| Basic Char and Pointer questions I'm approaching C from a background consisting of about 6 years worth of ActionScript, including AS3, so certain concepts like pointers and memory allocation are new to me. What's particularly confusing to me is the way C treates Strings as char arrays. Can anyone shed some light on why the following code yields the following results? Code: #include <cstdlib>
#include <iostream>
using namespace std;
// String Max Length.
#define FOOLEN 11
char* aString(char *str);
int main(int argc, char *argv[])
{
int safety;
char foo[FOOLEN] = "Foo";
/*
this assignment appears to have the same result as
the following uncommented assignment...
*/
// char *cp = foo;
char *cp;
*cp = foo[0];
cout << foo << endl << *cp << endl << aString(foo) << "\n\n";
strncpy(cp, aString("What is the meaning of this?"), FOOLEN - 1);
safety = FOOLEN - 1;
// But traversing the resulting char array tells a different story.
while(*cp && safety){
cout << cp << endl;
cp++;
safety--;
}
cout << safety;
system("PAUSE");
return EXIT_SUCCESS;
}
char* aString(char *str){
// I am unclear on why returning *str results in a compiler error.
return str;
}
char *cp = foo; and char *cp; *cp = foo[0]; Should have the same result, but it clearly does not. Furthermore, what if in the aString function I wish to return a modified version of the char array passed to it? I could instantiate a new char array and return that value, but it only contains the first character from the passed argument. The goal would be to create a duplicate of the char array passed to the function rather than to directly modify the char array, in the event that you want to preserve the original data. |
| maskedMan is offline | |
| | #2 |
| Jack of many languages Join Date: Nov 2007 Location: Katy, Texas
Posts: 2,071
| foo represents the pointer to the char array. foo[0] represents the first element in the array, not the address of the first element. When you code Code: char *cp = foo; When do code this: Code: *cp = foo[0]; If you wanted the address of the first element in foo to be assigned to thechar pointer, you would code Code: cp = &foo[0];
__________________ Mac and Windows cross platform programmer. Ruby lover. Last edited by Dino; 11-11-2009 at 10:35 AM. Reason: fix code and comments. |
| Dino is offline | |
| | #3 |
| Registered User Join Date: Sep 2004 Location: California
Posts: 3,020
| That's not right. You can't assign a pointer value to a char. Get rid of the '*'.
__________________ bit∙hub [bit-huhb] n. A source and destination for information. |
| bithub is offline | |
| | #4 |
| Jack of many languages Join Date: Nov 2007 Location: Katy, Texas
Posts: 2,071
| Oops - missed that. I know that. Thanks for the correction.
__________________ Mac and Windows cross platform programmer. Ruby lover. |
| Dino is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| problems with pointer and segfault in a very basic prog | melight | C Programming | 12 | 01-02-2009 07:24 AM |
| Basic program design, passing pointer to a function | heras | C Programming | 14 | 04-02-2008 03:21 AM |
| Direct3D problem | ahluka | Game Programming | 10 | 04-09-2006 03:36 AM |
| Could somebody please help me with this C program | brett73 | C Programming | 6 | 11-25-2004 02:19 AM |
| Contest Results - May 27, 2002 | ygfperson | A Brief History of Cprogramming.com | 18 | 06-18-2002 01:27 PM |