2 Replies - 588 Views - Last Post: 06 November 2009 - 05:51 PM Rate Topic: -----

#1 DramaKing  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 23-September 09

char array to char*

Posted 06 November 2009 - 05:30 PM

Hi, lets say I have a char array like this
char word[5] = "Hello";
How do I go about converting the char array to a pointer to char? (The pointer points to the whole word "Hello")

Thanks in advance.
Is This A Good Question/Topic? 0
  • +

Replies To: char array to char*

#2 hackterr  Icon User is offline

  • D.I.C Regular

Reputation: 21
  • View blog
  • Posts: 293
  • Joined: 13-August 09

Re: char array to char*

Posted 06 November 2009 - 05:47 PM

the variable word(without square brackets) itself is a pointer to the first character of your array
It cant point to the whole word as whole word is stored as characters stored in consecutive memory locations
since a pointer can point towards only one memory location
it points to the starting character of the string

This post has been edited by hackterr: 06 November 2009 - 05:49 PM

Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5951
  • View blog
  • Posts: 23,210
  • Joined: 23-August 08

Re: char array to char*

Posted 06 November 2009 - 05:51 PM

That character array needs to have 6 elements...one for the terminating null character.

And this:
char word[] = "Hello";
char *ptr = word;

gives you a pointer to char from that word.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1