-1

How to convert a char * to char.

I have a char pointer

char * Data which gets some value

I also have a variable

char result;

Can I do

result = *(Data)?

If I do so its throwing me a null pointer assignment.

I want to store the values in an array of result for different values of "Data" in a loop.

user1336997
  • 119
  • 1
  • 8
  • 8
    You seem to be pretty confused about the basics of C. I suggest "The C programming language" by Kernighan and Ritchie. Either way, ``result = *Data`` will get you the first char in Data, if it has any to begin with. – dimatura Apr 16 '12 at 20:48
  • 5
    It's difficult to answer this without more info. What is `data`? Is it a pointer to a single `char`, or a string? And it sounds like you have a more fundamental issue (hence the error), which we can't diagnose without seeing some relevant code. If you want specific help, I suggest creating a [minimal test case](http://sscce.org). – Oliver Charlesworth Apr 16 '12 at 20:49
  • Code's mostly [here](http://stackoverflow.com/questions/10180701/how-to-save-a-value-from-pointer-into-a-char-array) – Mat Apr 16 '12 at 20:50

2 Answers2

2

Your code is correct, but you need to check that "Data" may be null - which is why you're experiencing your null pointer assignment. Make sure you're using "Data" to iterate over your char array correctly.

Jonathan
  • 840
  • 6
  • 14
  • @kay: In all fairness, he did. There are some details missing but there is code there and if you look closely, the question is clear. He does not understand why he is getting an null pointer and wants to know if result = *Data is correct. –  Apr 16 '12 at 20:56
0

You didn't specify how long or what exactly is stored in Data.

char * normally points to an array of characters.

You are probably getting a null-pointer because char* Data has not been initialized with any string of data whatsoever.

result = *Data; will get you the first character, but you must first have data inside your Data variable to begin with