I have done the following, and I just want to check if I've done them all correctly:

  • Declare a pointer variable named fp that can point to a variable of type string.
  • Declare fish to be a 5-element array of strings.
  • Make the fp variable point to the last element of fish.
  • Make the string pointed to by fp equal to "salmon", using the * operator.
  • Without using the fp pointer, and without using square brackets, set the element at index 3 of the fish array to have the value "yellowtail"
string* fp;
char fish[5];
fp = &fish [4];
*fp = "salmon";
*(fp-1) = "yellowtail";

Declare fish to be a 5-element array of strings.

char fish[5];

You're close, but it said "5-element array of strings". You used char, so that would be incorrect. What you wrote just now is a string that can hold 5 characters, whereas they want you to declare a string array that can hold 5 elements.

Here are the rest: ( I have a feeling that the last two aren't right somehow)

  • Declare a pointer variable named fp that can point to a variable of type string.
string* fp;
  • Declare fish to be a 5-element array of strings.
string fish[5];
  • Make the fp variable point to the last element of fish.
fp = &fish [4];
  • Make the string pointed to by fp equal to "salmon", using the * operator.
*fp = "salmon";
  • Without using the fp pointer, and without using square brackets, set the element at index 3 of the fish array to have the value "yellowtail"
*(fp-1) = "yellowtail";
  • Move the fp pointer back by three strings.
fp -=3;
  • Using square brackets, but without using the name fish, set the element at index 2 of the fish array to have the value "eel".
*(fp+2) = "eel";
  • Without using the * operator, but using square backets, set the string pointed to by fp to have the value "tuna".
*fp = "tuna";
  • Declare a bool variable named d and initialize it with an expression that evaluates to true if fp points to the string at the start of the fish array, and false otherwise.
bool d;
    if (*f == & fish)
         return true;
    else 
         return false;
  • Using the * operator in the initialization expression, declare a bool variable named b and initialize it with an expression that evaluates to true if the string pointed to by fp is equal to the string immmediately following the string pointed to by fp, and false otherwise.
bool b;
    if (*fp == *(fp+1))
         return true;
    else
         return false;

Declare a pointer variable named fp that can point to a variable of type string.

string* fp;

That's correct as far as I can tell.

Declare fish to be a 5-element array of strings.

string fish[5];

Yep.

Make the fp variable point to the last element of fish.

fp = &fish [4];

Nothing wrong with that one.

Make the string pointed to by fp equal to "salmon", using the * operator.

*fp = "salmon";

Still good.

Without using the fp pointer, and without using square brackets, set the element at index 3 of the fish array to have the value "yellowtail"

*(fp-1) = "yellowtail";

Now on this one I'm a little bit confused. Are you sure the question is correct? At the moment, you're using fp to do the assignment, and if you didn't use that as instructed, the only thing you'd be left with to use is fish , which is useless if you can't use square brackets. And another thing: when they refer to "index 3", do they mean the third element in the array, or fish[3]? In any case, you'll need to move it back more than 1 element, which is what you're doing now.

Move the fp pointer back by three strings.

fp -=3;

Fine.

Using square brackets, but without using the name fish, set the element at index 2 of the fish array to have the value "eel".

*(fp+2) = "eel";

Hmm... I don't see you using []. You would use fp[index] .

Without using the * operator, but using square backets, set the string pointed to by fp to have the value "tuna".

*fp = "tuna";

Same problem as before. You're using * and not using []. Refer to my previous answer on how to fix this.

Declare a bool variable named d and initialize it with an expression that evaluates to true if fp points to the string at the start of the fish array, and false otherwise.

bool d;
    if (*fp == & fish)
         return true;
    else 
         return false;

Your expression *fp == & fish doesn't work. At the moment, you're trying to compare the string that fp points to to the memory address of fish. That is totally wrong. Also, you don't seem to be initalizing d ...

Using the * operator in the initialization expression, declare a bool variable named b and initialize it with an expression that evaluates to true if the string pointed to by fp is equal to the string immmediately following the string pointed to by fp, and false otherwise.

bool b;
    if (*fp == *(fp+1))
         return true;
    else
         return false;

I think the actual expression is correct, but you don't actually initalize b with the expression like the instructions say.

Without using the fp pointer, and without using square brackets, set the element at index 3 of the fish array to have the value "yellowtail"

Code:
*(fp-1) = "yellowtail";
Now on this one I'm a little bit confused. Are you sure the question is correct? At the moment, you're using fp to do the assignment, and if you didn't use that as instructed, the only thing you'd be left with to use is fish, which is useless if you can't use square brackets. And another thing: when they refer to "index 3", do they mean the third element in the array, or fish[3]? In any case, you'll need to move it back more than 1 element, which is what you're doing now.

// That is what the specifications say. I agree with you, if I can’t use the fp, or the square brackets, then what am I left with? I'm guessing (fish-1) = "eel"; Index 3 refers to fish[3]. I moved it back one element, since we were on element 4 (salmon) and it asked for index 3.

Using square brackets, but without using the name fish, set the element at index 2 of the fish array to have the value "eel".
Code:
*(fp+2) = "eel";
Hmm... I don't see you using []. You would use fp[index].

// I fixed it with:
Fp[2] = “eel”;

Without using the * operator, but using square backets, set the string pointed to by fp to have the value "tuna".
Code:
*fp = "tuna";
Same problem as before. You're using * and not using []. Refer to my previous answer on how to fix this.

// Fixed:
Fp[0] = “tuna”;

Declare a bool variable named d and initialize it with an expression that evaluates to true if fp points to the string at the start of the fish array, and false otherwise.
Code:
bool d; if (*fp == & fish) return true; else return false;
Your expression *fp == & fish doesn't work. At the moment, you're trying to compare the string that fp points to to the memory address of fish. That is totally wrong. Also, you don't seem to be initalizing d...

// How would I initialize d? It probably is something easy that I forgot.

Also, there is my fixed code:
if (fp) // if fp points to first element of array
Return true;
else
Return false;

Using the * operator in the initialization expression, declare a bool variable named b and initialize it with an expression that evaluates to true if the string pointed to by fp is equal to the string immmediately following the string pointed to by fp, and false otherwise.
Code:
bool b; if (*fp == *(fp+1)) return true; else return false;
I think the actual expression is correct, but you don't actually initalize b with the expression like the instructions say.

// Again, how do I initialize b?

<< // Again, how do I initialize b?

bool b = TRUE;
    if (*fp == *(fp+1))
         return true;
    else
         return false;

Ternary operators anyone?

b = (*fp == *(fp + 1)) ? true : false ;

Use the same thing for d and it should be fine.

variable_name = (condition) ? (if_condition_true_return_this_value) : (else_return_this_value)

Alright, I changed all of them.
I do have a problem in #5.

Would

*(fish+3);

by correct?

Alright, I changed all of them.
I do have a problem in #5.

Would

*(fish+3);

by correct?

Actually, you're right. I just thought of that a moment ago, and yes that will work. You should be all good to go now.

Cool, I have another task that I wonder if it's correct:

  • Rewrite the following function so that it returns the same result, but does not increment the variable ptr. Your new program must not use any square brackets, but must use an integer variable to visit each double in the array. You may eliminate any unneeded variable.
  • double computeAverage(const double* scores, int numScores)
        {
            const double* ptr = scores;
            double tot = 0;
            while (ptr != scores + numScores)
            {
                tot += *ptr;
                ptr++;
            }
            return tot/numScores;
        }
    [*]

Here's my answer:

double computeAverage(const double* scores, int numScores)
    {
        const double* ptr = scores;
        double tot = 0;
        for (ptr = scores; ptr <numScores; ptr++)
        {
            tot += *ptr;
        }
        return tot/numScores;
    }

wouldn't the "fish" array be a good place to use "enum"?, Then just use an integer array?

Can you give an example? I don't think I was taught enum.

wouldn't the "fish" array be a good place to use "enum"?, Then just use an integer array?

No.

The exercise given to the OP was to strengthen the concepts regarding pointers and the likes. There is as such no real program here. And as far as the fish types are concerned -- you are better off using normal char strings. And I dont understand what you mean by using enumns and then integer arrays ? Care to elaborate ?

@aznballerlee

You can find out more about [search]enums in c c++[/search] on google. For the time being just concentrate on your excecises -- you are doing really fine.

Ahh I would just like opinion on the correctness of what I've done.
Hopefully it looks right .

Never mind, I fixed those problems.

I'm now trying to transform

const char* findTheChar(const char str[], char chr)
    {
        for (int k = 0; str[k] != 0; k++)
            if (str[k] == chr)
                return &str[k];

        return NULL;
    }

without square brackest or integer variables. Would this be a correct implementation?

const char* findTheChar(const char str[], char chr)
    {
       while (*str != 0)
       {
          if (*str == chr)
             return str;
          str ++;
       }
       return NULL; 
    }

Yes, it works fine.

PS: I was just wondering why you ask these questions in the forums, surely you can try compiling the program, runnign it and see whether it works or not or can't you ?

I have tried on the compiler; I'm just in a rush since this is due today.
I do have a problem that I can't test on the compiler (this is one where I have to explain output)

I'm sure something is wrong in my logic somewhere.

In this, I have to explain each output. I ran it throguh the compiler and my steps didn't match what it spit out:

#include <iostream>

    using namespace std;

    int* minimart(int* a, int* b)
    {
        if (*a < *b)
            return a;
        else
            return b;
    }

    void swap1(int* a, int *b)
    {
        int* temp = a;
        a = b;
        b = temp;
    }

    void swap2(int* a, int *b)
    {
        int temp = *a;
        *a = *b;
        *b = temp;
    }

    int main()
    {
        int array[6] = { 5, 3, 4, 17, 22, 19 };

        int* ptr = minimart(array, &array[2]);
        ptr[1] = 9;
        ptr += 2;
        *ptr = -1;
        *(array+1) = 79;

        cout << "diff=" << &array[5] - ptr << endl;

        swap1(&array[0], &array[1]);
        swap2(array, &array[2]);

        for (int i = 0; i < sizeof(array)/sizeof(*ptr); i++)
            cout << array[i] << endl;

        double *dp;

        if (sizeof(dp) < sizeof(ptr))
            cout << "double pointers are smaller than int ptrs\n";
        else if (sizeof(dp) > sizeof(ptr))
            cout << "double pointers are larger than int ptrs\n";
        else
            cout << "double pointers are the same size as int ptrs\n";

The compiler gave me this:

diff = 1
4
79
5
9
-1
19
double pointers are the same size as int ptr

In my next post, I'll explain my steps so I guess someone can spot my errors?

int array[6] = { 5, 3, 4, 17, 22, 19 };
        int* ptr = minimart(array, &array[2]);
        ptr[1] = 9;
        ptr += 2;
        *ptr = -1;
        *(array+1) = 79;

        cout << "diff=" << &array[5] - ptr << endl;

For this, I changed my array to get {5, 79, 4, 9, 22, -1}

The first part in main

int* ptr = minimart(array, &array[2]);

returned me b (which I interpreted to be 2)

Then, since ptr is pointing to element 2,

ptr[1] = 9;

would set array[3] to 9.

ptr+= 2

moved the pointer to array[5], and the next step

*ptr = -1;

set array[5] to -1.

*(array+1);

sets array[1] to 79, since array was pointing to the beginning of the array.

Last, I outputted diff, which was 6-5 = 1 (5th element of array) pointed to array[6] - ptr (which points at array[5]).

I moved on to:

swap1(&array[0], &array[1]);
 swap2(array, &array[2]);

I concluded that
void swap 1 would swap array [1] and array[2].

void swap1(int* a, int *b)
    {
        int* temp = a;
        a = b;
        b = temp;
    }

and void swap 2 to swap elements 1 and 3.

void swap2(int* a, int *b)
    {
        int temp = *a;
        *a = *b;
        *b = temp;
    }

So after this, I would have {5, 79, 4, 9, 22,-1}
This doesn't match up with what the compiler spit out,
so there must be something wrong!

Keep tab or print out the array after each iteration to give yourself a better understanding of what exactly is going on.

BTW your first swap function doesnt do anything, I'll leave it to you to figure it out.

I think I have the stuff up to the swap function okay.
I'm guessing that the first swap function only switches the address of the array[0] and array[1]?

So that's why it does nothing?

And then swap 2 swaps the elements inside array[0] and array[2]?

Yes, the first swap function only switches addresses. If you have got that figured then the confusion should end and you should be getting the correct answer.

Okay, up to the swap functions, I have {5, 79, 4, 9, 22, -1}

If I do swap 1, nothing changes.
Doing swap 2 would swap elements array[0] and array [2]
Since array was pointing at array [1] before,
array [0] = 79
array [2] = 9

Swapping those would get me {5, 9, 4, 79, 22, -1}

I'm supposed to get {4, 79, 5, 9, --1, 19}

I'm sure what I've done before the swaps were fine .. :sad:

I have told you in my previous to previous post to print out the result after each operation so that we can easily spot the problem..you still havent done it.

Paste the result after each execution here and you will your self be able to find the flaw.

Hey I worked it out, and I understood my problem. My problem was location of the pointers, thanks for telling me to work it out again.

I do have one remaining problem.

double *dp;

        if (sizeof(dp) < sizeof(ptr))
            cout << "double pointers are smaller than int ptrs\n";
        else if (sizeof(dp) > sizeof(ptr))
            cout << "double pointers are larger than int ptrs\n";
        else
            cout << "double pointers are the same size as int ptrs\n";

dp points to doubles, so it has 8 bytes.
ptr points to integers, which has only 4 bytes.

How come I get the else statement, that

"double pointers are the same size as int ptrs

?

Just to strengthen your basics a bit, write a small code which declares pointers of char, int and double data type and print out their sizes.

The results would be interesting to note...

#include <iostream>
using namespace std;

int main ()
{
    double *dp;
    int *dq;
    char *dw;

    int a = sizeof(dp);
    int b=  sizeof (dq);
    int c =  sizeof (dw);

    cout << a << b << c << endl;
    return 0;
}

I get 4 4 4 .. interesting! Why is this? In my notes, I have down that
int has 4 bytes, doubles have 8 bytes.

The sizes you are talking about are the size of data types.

int data type holds integer values, in laymans terms numbers which dont have decimals in them and they take up consist of 4 bytes.

char data types hold ASCII values which are 8 bits ( 1 bytes ) hence size of character is one byte and so on..

But the point here is that pointers are "addressing mechanisms" and pointer variables "store addresses". If you keep these two highlihghed points in mind you should never have confusion regarding size of pointers.

And the size of pointers is 4 bytes ( 32 bits ) since you are working on a 32 bit platform / OS . It has alot to do with processor architecture, internal registers, memory addressing...

Some basic pointer tutorials here.

I don't find anything wrong with the last one...
Its good to go..

my bad, sorry. So many concepts...so few brain cells...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.