C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-11-2009, 10:03 AM   #1
Registered User
 
Join Date: Nov 2009
Posts: 1
Basic Char and Pointer questions

Hello,

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;   
}
As far as I can tell from syntax:

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   Reply With Quote
Old 11-11-2009, 10:22 AM   #2
Jack of many languages
 
Dino's Avatar
 
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;
you are assigning the char array pointer (foo) to the char pointer. Correct.

When do code this:
Code:
*cp = foo[0];
you are assigning the first element (the actual value contained in foo[0]) to a pointer that has not been initialized. Wrong.

If you wanted the address of the first element in foo to be assigned to thechar pointer, you would code
Code:
cp = &foo[0];
using the & to get the address of the first element.
__________________
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   Reply With Quote
Old 11-11-2009, 10:31 AM   #3
Registered User
 
Join Date: Sep 2004
Location: California
Posts: 3,020
Quote:
Originally Posted by Dino View Post
If you wanted the address of the first element in foo to be assigned to thechar pointer, you would code
Code:
*cp = &foo[0];
using the & to get the address of the first element.
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   Reply With Quote
Old 11-11-2009, 10:33 AM   #4
Jack of many languages
 
Dino's Avatar
 
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   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 03:39 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22