pointer to array element

Hello , everyone I just have a quick question as I cant find much on google about it. How would I go about having a pointer to an array element specificity a character in a c-string.Every thing I try will not even build.An array is already a pointer to the first location of the array right? Here is what I have tried if some could just let me know if I am on the right track. This is for a homework assignment. Thanks guys.
1
2
3
4
5
6
7
8
9
10
11
12
        char *pHead;    
        char *pTail;
	
        pHead = sentence[0];   <=== This wont build
	pHead = &sentence[0];
        pHead = sentence[0]*;
        *pHead = sentence[0]; <===== this builds but is not storing anything




Last edited on
What type of object is sentence?
Did you initialize sentence[0]? Could explain why nothing is stored on line 7.

On line 4, you are attempting to assign a char to a pointer (I am assuming from what you said that sentence is a c-string, or char[]). Yes, an array is basically a pointer, but the member access operator ( [] ) dereferences the pointer. What you want is pHead = sentence. This will assign pHead with the address pointed by sentence.
sorry guys that was just a snippet of code here is everything thanks for the reply.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64

#include <iostream>
//#include <string.h>
//#include <string>


char* getInputFromUser(char*);
//pass in pointer to cstring
//populate return pointer to it

char* reverseCString(char*);
//
//


int main( ) 
{
	char sentence[20];
	char *pSentence;
	pSentence = getInputFromUser(sentence);
	reverseCString(pSentence);
	
  
	return 0;
}

char* getInputFromUser(char *sentence)
{
	std::cout << "Enter a sentence to reverse no more than 20 characters long.\n"
			<< " (anything past 19 characters will be truncated)\n";
    std::cin.getline(sentence, 20);
	
	return sentence;
}

char* reverseCString(char* sentence)
{
	char *pHead;
	pHead = sentence;
	char *pTail;
	//pTail = sentence[18];
	std::cout << pHead << std::endl;
	//std::cout << pTail << std::endl;
	
// 	for (int i =0; i < 20; i++)
// 	{
// 		 
// 	}
	
}






//  Write a program that accepts a C-string input from the user and reverses the con-
//  tents of the string. Your program should work by using two pointers. The "head"
//  pointer should be set to the address of the first character in the string, and the
//  "tail" pointer should be set to the address of the last character in the string (i.e., the
//  character before the terminating null). The program should swap the characters
//  referenced by these pointers, increment "head" to point to the next character, dec-
//  rement "tail" to point to the second-to-last character, and so on, until all characters
//  have been swapped and the entire string reversed. 
Last edited on
In your reverseCString function, you never initialize the address pHead should be pointing to.
Line 39 is a bit odd... *pHead will be dereferencing so random location since you never initialized pHead. The compiler should spit out an error. It should also complain with *sentence[0]. Think of the array this way: sentence, the name of the array, is actually a pointer to the first item, or sentence[0]. The square brackets are a way to dereference the pointer by some offset, so sentence[0] will already return the first value in the array. Attempting to dereferencing a char will not work.
If you want pHead to point to the first item in sentence, merely assign the memory address stored in sentence, i.e. pHead = sentence. Now you can do either of the following to access the characters:
1
2
3
4
5
6
7
8
9
10
11
char* pTail = sentence + 19;    //Initialize pTail with the address of sentence's last char
   //Note that this and pTail = sentence + 19 are the same thing.
   //Using sentence here because pTail is not really necessary
for(int I = 20; I > 0; I--)
   std::cout << sentence[I-1];  // operator[]
//or
for(int I = 20; I > 0; I--)
   std::cout << *(sentence + I - 1);  // operator*
//with pTail
for(int I = 0; I < 20; I++)
   std::cout << *(pTail - I);

This code has not been tested, so it probably has bad style and bugs.

Edit: Fixed up my example code because I can't stand the nub mistakes I made.
Last edited on
Thanks alot Daleth that helped alot.
Topic archived. No new replies allowed.