Advertisement

char and char*

Started by June 04, 2002 10:02 PM
6 comments, last by Ragadast 22 years, 6 months ago
What''s the difference betweena char and a char*? I know that i can''t use both for the same things, but I don''t get what the difference is.
The may difference is that declaration:
char m = ''m'';
char *mystring = "Here is your string";

char * stores a pointer to a string you give, while char declares a letter you store. An example:

#include <stdio.h>
char m = ''m'';
char *string = "Here I am";


void main(void)// Find letter ''m'' position in array ''string''
{

for (int i=0; i{
if(string == m)
printf("Letter %c is at position %d", m, i);
}
}


" Do we need us? "


Ionware Productions - Games and Game Tools Development

Advertisement
char is just that, a character. char * is a pointer to a character.

/*=========================================*/
/* Chem0sh */
/* Lead Software Engineer & Tech Support */
/* http://www.eFaces.biz */
/*=========================================*/
/*=========================================// Chem0sh// Lead Software Engineer & Tech Support// http://www.eFaces.biz=========================================*/
And so when you do
 char *name = "hello"  

and not
char name2[] = "wazzup!!";char *name3 = &name2 

is skiping out the second part because "name" string is on the stack so you are just refering to it striaght of there instead of copying it into an array or something?

CEO Plunder Studios
[email=esheppard@gmail.com]esheppard@gmail.com[/email]
char * str = "hello"; 


and

char str[] = "hello"; 


Do essentially the exact same thing.

elis-cool, your last line of code is incorrect for what you''re trying to. Just the variable name "name2" is a pointer to the first element of the array. So assigning a char array to a char * is pointless (no pun intended).
/*=========================================// Chem0sh// Lead Software Engineer & Tech Support// http://www.eFaces.biz=========================================*/
Chem0sh try this (MSVC++ 6.0):
char * str1 = "hello"; char str2[] = "hello";*str1 = ''y''; // Access Violation.*str2 = ''y''; // Works fine. 
Why? str1 points to the constant string "hello", while str2 is a copy of the constant string "hello".
Arguing on the internet is like running in the Special Olympics: Even if you win, you're still retarded.[How To Ask Questions|STL Programmer's Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]
Advertisement
Not to mention this:

str1 = "hello again"; //Works finestr2 = "hello again"; //Error: left side of = must be an l-value 


Kippesoep
From:
http://cplus.about.com/library/weekly/aa040702a.htm:

Pointers are variables that hold addresses in C and C++. They provide much power and utility for the programmer to access and manipulate data in ways not seen in some other languages. They are also useful for passing parameters into functions in a manner that allows a function to modify and return values to the calling routine. When used incorrectly, they also are a frequent source of both program bugs and programmer frustration.

Introduction
As a program is executing all variables are stored in memory, each at its own unique address or location. Typically, a variable and its associated memory address contain data values. For instance, when you declare:

int count = 5;

The value "5" is stored in memory and can be accessed by using the variable "count". A pointer is a special type of variable that contains a memory address rather than a data value. Just as data is modified when a normal variable is used, the value of the address stored in a pointer is modified as a pointer variable is manipulated.

Usually, the address stored in the pointer is the address of some other variable.

int *ptr;
ptr = &count /* Stores the address of count in ptr */
/* The unary operator & returns the address of a variable */

To get the value that is stored at the memory location in the pointer it is necessary to dereference the pointer. Dereferencing is done with the unary operator "*".

int total;
total = *ptr; /* The value in the address stored in ptr is assigned to total */

The best way to learn how to use pointers is by example. There are examples of the types of operations already discussed below. Pointers are a difficult topic. Don''t worry if everything isn''t clear yet.

Declaration and Initialization
Declaring and initializing pointers is fairly easy.


main()
{
int j;
int k;
int l;
int *pt1; /* Declares an integer pointer */
int *pt2; /* Declares an integer pointer */
float values[100];
float results[100];
float *pt3; /* Declares a float pointer */
float *pt4; /* Declares a float pointer */

j = 1;
k = 2;
pt1 = &j /* pt1 contains the address of the variable j */
pt2 = &k /* pt2 contains the address of variable k */
pt3 = values; /* pt3 contains the address of the first element of values */
pt3 = &values[0]; /* This is the equivalent of the above statement */

}

Pointer Dereferencing/Value Assignment
Dereferencing allows manipulation of the data contained at the memory address stored in the pointer. The pointer stores a memory address. Dereferencing allows the data at that memory address to be modified. The unary operator "*" is used to dereference.
For instance:

*pt1 =*pt1 + 2;



This adds two to the value "pointer to" by pt1. That is, this statement adds 2 to the contents of the memory address contained in the pointer pt1. So, from the main program, pt1 contains the address of j. The variable "j" was initialized to 1. The effect of the above statement is to add 2 to j.

The contents of the address contained in a pointer may be assigned to another pointer or to a variable.

*pt2 = *pt1; /* assigns the contents of the memory pointed to by pt1 to the contents of the memory pointer to by pt2;
k = *pt2; /* assigns the contents of the address pointer to by pt2 to k. */


Pointer Arithmetic
Part of the power of pointers comes from the ability to perform arithmetic on the pointers themselves. Pointers can be incremented, decremented and manipulated using arithmetic expressions. Recall the float pointer "pt3" and the float array "values" declared above in the main program.

pt3 = &values[0]; /* The address of the first element of "values" is stored in pt3*/
pt3++; /* pt3 now contains the address of the second element of values */
*pt3 = 3.1415927; /* The second element of values now has pie (actually pi)*/
pt3 += 25; /* pt3 now points to the 26th element of values */
*pt3 = 2.22222; / The 26th element of values is now 2.22222 */

pt3 = values; /*pt3 points to the start of values, now */
for (ii = 0; ii < 100; ii++)
{
*pt3++ = 37.0; /* This sets the entire array to 37.0 */
}
pt3 = &values[0]; /* pt3 contains the address of the first element of values */
pt4 = &results[0]; /* pt4 contains the address of the first element of results */
for (ii=0; ii < 100; ii++)
{
*pt4 = *pt3; /*The contents of the address contained in pt3 are assigned to
the contents of the address contained in pt4 */
pt4++;
pt3++;
}




Never think you are great, for you did not create yourself.

This topic is closed to new replies.

Advertisement