Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Coverting a char pointer to char

Status
Not open for further replies.

omaduro

Technical User
May 29, 2002
3
AW
Does anyone know how to convert a char* to char.
 
Hi omaduro.

To answer this, it depends on the context. To illustrate each context I can think of I wrote the program below.
Try it out to see what I mean...
Code:
#include <stdio.h>
#include <string.h>

int main (void)
{
  char ch = 'A'; // single character.
  char arr[10];  // array of characters.
  char *ptr;     // pointer to character/array.

  strcpy(arr, &quot;Hello!&quot;);
  printf(&quot; ch=%c \n arr=%s \n&quot;, ch, arr);

  ptr = arr;  // Pointer to string.
  printf(&quot; ptr=%s&quot;, ptr);
  ptr = (char *)ch; // Pointer to a char.
  printf(&quot; ptr=%c\n&quot;, ptr);
  ptr = arr;     // Shows how to manipulate chars in
  *(ptr+1) = ch; // an array(string).
  printf(&quot; ptr=%s&quot;, ptr);

  return 0;
}
I suspect that you meant th pointer to a single char, that required a type-cast (to char *) in the example.

Hope that explains it. :)

Adonai
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top