0

I am new to C++ but have many years of programming. All my learning has been very good until I reached pointers. I must admit am struggling to accomplish simple stuff. For example, after failing to store array data into pointers - and then list the pointers, I went basic with below to help me understand. In this, I am aiming to enter a list of names into array pointer (or something like that), then retrieve the list the values from the memory location. a) I am not able to put this to allow request input and store to pointer b) when I loop through a pointer, it only displays the first characters of the names. If someone can help me achieve the above, it will be my BIGGEST breakthrough in pointers.

please help;

CODE

#include "stdafx.h"
#include<iostream>
#include<cstring>
#include<cctype>
using namespace std;

int i=0;
int main(){
    char* studentNames[6]={"John Xyy","Hellon Zzz","Wendy Mx","Beth Clerk", "Jane Johnson", "James Kik"};
    int iloop = 0;
    //loop through and list the Names
    for(iloop=0;iloop<6;iloop++){
        cout<<"Student :"<<studentNames[iloop]<<"               Addr:"<<&studentNames[iloop]<<endl;
    }
    cout<<endl;
    system("pause");
    //Now try and list values stored at pointer memory location
    int p=0;
    for (p=0;p<6;p++){
        cout<<*studentNames[p];
    }
    cout<<endl;
    system("pause");
    return(0);

}
Joshua McKinnon
  • 24,489
  • 11
  • 57
  • 63
Sylvester
  • 125
  • 1
  • 2
  • 8

1 Answers1

0

a) I am not able to put this to allow request input and store to pointer

Use fgets() to read strings from stdin as follows. You have array of size 6 pointers to characters. Assuming the strings length to 100 characters each, for each string read from stdin allocate buffer memory and copy it to studentNames array of pointers everytime.

char *studentNames[6];
char input[100];

for (int str = 0; str < 6; str++) {
    if (fgets(input, sizeof(input), stdin) != NULL) {
         studentNames[str] = (char *)malloc(strlen(input));
         strcpy(studentNames[str], input);
    }
}

b) when I loop through a pointer, it only displays the first characters of the names.

//Now try and list values stored at pointer memory location
int p = 0;
for (p = 0; p < 6; p++){
    cout<<*studentNames[p];  
}

In the above string printing, *studentNames[p] dereferences pth array of pointer each time in loop which prints first character only. Replace it with studentNames[p] which prints string literals.

Sunil Bojanapally
  • 12,528
  • 4
  • 33
  • 46
  • If I remove the pointer operator * on the last line, what is the difference then, if I remove all pointer * operator. Won't this turn this into a purely array solution, beating the motivation foe pointers. – Sylvester Nov 07 '13 at 18:41
  • @Sylvester It is how you access elements in array of pointers. In `int *p[]`, accessing elements is done by `p[i][j] is one char; p[i] is char *; & p is char **`. Check out my previous [SO answer](http://stackoverflow.com/questions/19325240/difference-between-char-p-char-p-char-p/19764065#19764065) for more information. – Sunil Bojanapally Nov 07 '13 at 18:51
  • I don't understand the value STDIN I am also getting an error on studentNames[str] = malloc(strlen(input)); that " value of void * cannot be assigned to entity type char *" – Sylvester Nov 07 '13 at 19:15
  • The error basically says it an invalid conversion form malloc() returned type `void *` to `char *`. Just typecast malloc() result as `(char *)malloc(strlen(input));`. And `stdin` should be in small letters, is standard input stream. – Sunil Bojanapally Nov 07 '13 at 19:54