LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-12-2004, 11:41 AM   #1
djgerbavore
Member
 
Registered: Jun 2004
Location: PA
Distribution: Fedora (latest git kernel)
Posts: 458

Rep: Reputation: 30
pointer question,


i'm trying to learn pointers, and i have a simple question,

Code:
                  char str[10] = "hello\0";
                  char *ptr;
                  ptr = &str;
                  printf("%s\n", ptr);
when i can a compile error: incompatiable pointer type.

however when i do this:
Code:
                 char str[10] = "hello\0";
                 char *ptr;
                  ptr = str;
                  printf("%s\n", ptr);

it compiles and works.

my question is don't you have to reference a pointer before you use it, or do you need to reference an index in an array, like ptr = &str[0].

thanks
 
Old 08-12-2004, 11:53 AM   #2
Ankheg
Member
 
Registered: Jul 2004
Location: Woodland Hills, CA
Distribution: Debian/Mandrake
Posts: 37

Rep: Reputation: 15
Lets see if this helps...

Say we have this definition:
char ptr = 'A';

Given this, *ptr is not the same as &ptr.

*ptr will evaluate to the character 'A', whereas &ptr would evaluate to the memory address at ptr.


Another thing that needs to be known is these two definitions will be the same...

char *str = "Hello\0";
char str[6] = "Hello\0";

A string in C shouldn't really be thought of as a string, but rather an array of char...


Sorry I don't have time to put more on the subject, but work is nagging at me... Hope it helps though.
 
Old 08-12-2004, 11:55 AM   #3
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
because of the way arrays work str is a char *const, which points to the first element of the 10 char array. the [] notation works like str[z]=*(str+z) so ptr=&str[0] works out as the address of the first character of str ie str, this is shown below

ptr=&str[0]
ptr=&*(str+0)
ptr=&*(str)
ptr=str

if you remember that arrays are just pointers that point to the start of the array and you remember that x[z]=*(x+z) then you should have no problems.

<edit>
by Ankheg
Say we have this definition:
char ptr = 'A';

Given this, *ptr is not the same as &ptr.

*ptr will evaluate to the character 'A',


no it wont, *ptr makes no sense as ptr isnt a pointer

whereas &ptr would evaluate to the memory address at ptr.
&ptr would evaluate to the memory address of ptr

Another thing that needs to be known is these two definitions will be the same...

char *str = "Hello\0";
char str[6] = "Hello\0";


NO, these definitions must be thought of as very different, the first creates a 4 byte pointer on the stack and puts in it the address of the string "Hello\0" which is normally stored in a read only area. the second creates a 6 byte array on the stack(which is not big enough to hold your string btw) and copies into it the string "Hello\0". the two statements are very very different
</edit>

Last edited by kev82; 08-12-2004 at 12:06 PM.
 
Old 08-12-2004, 12:01 PM   #4
max_sipos
Member
 
Registered: Jul 2004
Posts: 96

Rep: Reputation: 15
str is an array of chars. In C, an array of type is just a type *; str[n], jjust translates to: *(str+sizeof(char)*n) ..

So you could have written just

char * str = "hello" ;

and defined like that you can use str[3] just as well.

BTW, " " quotes denote a null terminated string so you do not need to put a \0 on the end (it is assumed) of the string (unless, although I doubt it, you want a double-null-terminated string). Therefore "hello" is the same as 'hello\0'

Hope that helps,
Maksim Sipos
 
Old 08-12-2004, 02:46 PM   #5
MadCactus
Member
 
Registered: Jul 2003
Distribution: Slackware 9.1
Posts: 195

Rep: Reputation: 30
I think you can put it even more simply by saying that you can't use the & operator to get the address of a constant. The array base 'str' is a constant, although you can refer to it using pointer notation.
 
Old 08-12-2004, 09:07 PM   #6
coldAndUgly
Member
 
Registered: Dec 2003
Location: New Zealand
Distribution: Slackware 10.2
Posts: 36

Rep: Reputation: 15
Hi, in your program both,

Code:
str;
&str[x];
are of type pointer to character, where 0 <= x < number_of_elements. On the other hand,

Code:
&str;
is of type pointer to array of character. If you want a variable of type pointer to array of character you can do something like this:
Code:
#include <stdio.h>
#include <stdlib.h>

typedef char (*arr_ptr)[];

int main(int argc, char **argv)
{
	char string[] = "Hello";
	arr_ptr ptr;

	ptr = &string;
	printf("%s\n", *ptr);

	exit(EXIT_SUCCESS);
}
gcc -Wall -pedantic-errors -o ptr_test ptr_test.c

Here's a link to the best online text regarding C I've ever found.
The C book
 
Old 08-13-2004, 02:21 AM   #7
cppkid
Member
 
Registered: Jul 2004
Location: Pakistan
Distribution: Ubuntu
Posts: 185

Rep: Reputation: 30
To be very simple is that.

case 1: char *ptr; // "ptr" is pointer but no memory is allocated to it.
case 2: char ptr[10]; // "ptr" is a pointer and 10 bytes are allocated to it.

When you allocate memory to first case.
char *ptr;
ptr = new char[10];

Now you can say that is equal to the second case.

Just consider in case two we say "ptr" is a pointer but there is no " * " in its defination. Just suppose that ,
[ ] == *
you can not use both at once to declare a one dimentional pointer.
if you define,
char *ptr[10];
this means its a double pointer, Coz it has both [ ] and a *.

_________________________________________________________

Now comming to your question, why ptr = &str not work but ptr = str works.
The reason is that in your case,
ptr is a pointer (contains an address).
str is a pointer (contains an address).
When you say ptr = str. Its ok.
but when you say ptr = &str. It means that ptr will hold the address of str, and str itself contain an address, and you are not copying the content of str to ptr but the address of str to ptr.

Also keep in mind when you define
char str[10];
the variable str contains the address of the first location of the allocated memory.
As i previously mentioned that * == [ ], and as you know that to see the value of a location we use *, like ,

int *ptr;

output of ptr will give you an address.
outout of *ptr will give you the value stored at that address.

so when you hav char str[10];

and you want to see the value at first location you will write.
print str[0];
as [ ] == * so you can also write
print *str;

As i said that str contains the address of first locatin so both will print the same. aslo you can check it by,

print str[1] ===== print *(str + 1)
print str[4] ===== print *(str + 4)


so str is basically a pointer that contains the address of the first location of array and you want your pointer ptr to contain that address, you just write
ptr = str



Hope you will understand
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
String Pointer Question (c/c++) greecesking Programming 2 08-26-2004 06:07 AM
pointer to pointer question in c lawkh Programming 2 01-29-2004 10:26 AM
Pointer Question AMMullan Programming 15 01-10-2004 05:40 PM
Another C pointer question chris.hicks Programming 3 11-11-2003 04:38 AM
yet another C pointer question fatman Programming 3 04-18-2003 04:59 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:08 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration