Okay, I am working on a homework assignment for my class in which we are supposed to use pointers for all of the arrays in the functions. Here is what I have so far for the printArray function, which is supposed to print the members of the array horizontally. It is printing out nonsense right now: HPX'HPX'HPX. And then the program is having an error and terminating. What am I doing wrong? Also, how do I use pointers to do this, because right now I'm not using them?

void printArray(double * array, int what)
{
     int i;
     
     for (i=0; i < sizeof(array); i++)
     {
         printf("%c",& array[i]); 
     }
}

Recommended Answers

All 17 Replies

Try changing this:

printf("%c",& array[i]);

to

printf("%d",& array[i]);

%c is for a char, your array is of the type double.

http://tinyurl.com/n5lq4w

Neither %c nor %d are for double.

Okay. So I went to the link you posted, and looked at a few of the manuals, but I am still confused. Do I need to use one of the other forms of printf, such as vprintf? Or if not, what do I need to do?

Also, can you guys help me figure out how to use pointers for this function? Just a suggestion as to where to use them would be great, if anyone could provide that. I'll try and work from there.

Thank you guys for your help with this!

sizeof(array)/sizeof(double) ?

sizeof(array)/sizeof(double) ?

Huh? Is that how I use pointers for the function? I'm confused now...

Here is what I have for the function right now:

void printArray(double * array, int size)
{
     int i;
     
     for (i=0; i < sizeof(array); i++)
     {
         printf("%e",& array[i]); 
     }
}

It is printing some random numbers that are not what is entered, and then it has an error and makes me exit. Why is it doing that?

Okay, I have worked on my function a bit, and here is what I have right now:

void printArray(double * array, int size)
{
     int i;
     
     for (i=0; i < size; i++)
     {
         printf("%g", *(array+i), " ", "\n"); 
     }
}

It is now printing the correct numbers, but for some reason it is not putting space between them, or adding a new line after all of the numbers like it should. What am I doing wrong here?

Also, it is still giving me an error and making me exit the program after giving the output. How can I fix this?

Thank you guys for helping me!!

#include <stdio.h>

void printArray(double * array, int size)
{
   int i;
   for ( i = 0; i < size; i++ )
   {
      printf("%g \n", *(array+i));
   }
}

int main()
{
   double array[] = {123.456,-43.21};
   printArray(array, sizeof array / sizeof *array);
   return 0;
}

/* my output
123.456
-43.21
*/

Thank you for that alteration, Dave. I regret to say that what I actually need is for the output to be formatted as follows:
123.456 -43.21

And then multiple operations are done on the array, and the output from these operations are printed on the next few lines. I think I accomplished it, however:

void printArray(double * array, int size)
{
     int i;
     
     for (i=0; i < (int) size; i++)
     {
         printf("%g ", *(array+i), " "); 
     }
}

However, I am still having two problems with the code. For some reason, it is still giving me an error and making me terminate the program after the output is given. I have attached a picture of what happens after running the program. Also, for some reason, on the next few lines of output, the end of the last number doesn't display. You can see this in the attached picture too, in the window on the left. The second output line is supposed to be the array reversed, and the third line is supposed to be the array tripled, but it is not printing out the entire array.

Can anyone help me figure out why my program is still having these two problems?

Thank you guys for helping me get this right!

Below is the entire code for the whole program, in case that is part of what is going wrong.

/****
Assignment3.c
****/

#include <stdio.h>
#include <ctype.h>


void tripleEach(double * array, int size)
{
     int i;
     
     for (i = 0; i < size; i++)
     {
         array[i]=3 * array[i];
     }       
}


void reverse(double * array, int size)
{
    int a=0;
    int swap;

    for(a;a<--size;a++)
    {
        swap=array[a];       
        array[a]=array[size];    
        array[size]=swap;       
    }
}

 
void printArray(double * array, int size)
{
     int i;
     
     for (i=0; i < (int) size; i++)
     {
         printf("%g ", *(array+i), " "); 
     }
}


int main()
{
    int i;
    double * numArray;
    int size;
    double num;

    printf("Please enter a number of floating numbers to be entered:\n");
    scanf("%d", &size);

    malloc(size);

    for (i=0; i<size; i++)
    {
       scanf("%lf", &num);  //read in a double entered by user
       *(numArray+i) = num;
    }
 
    printArray(numArray, size);
    printf("\n");
    reverse(numArray, size);
    printf("\n");
    printArray(numArray, size);
    printf("\n");
    tripleEach(numArray, size);
    printf("\n");
    printArray(numArray, size);

    return 0;
}

Interesting stuff. Thank you

All formatting is in the first string.

printf("%g ", *(array+i), " "); /* main.c:40: warning: too many arguments for format */

You can't do that.

This maybe?

for (i=0; i < (int) size; i++)
     {
         printf("%g ", *(array+i));
     }
     purchar('\n');

Here:

malloc(size);

You may want to read the man page for malloc too.

numArray = malloc(size * sizeof *numArray);

Without the above line, numArray would still be a dangling pointer. Also note that in the call, you need not only the number of elements, but the size of each element.

You should check that malloc succeeds and free(numArray); when you're done with it. The prototypes for these are in #include <stdlib.h> .

If your compiler isn't showing you warning to help guide you, it would be beneficial to try to crank up the warning level. The compiler itself tries to answer these questions for you.

[edit]And to correctly do the swap, you'll want to use a double for the temp (not an int ).

Do you know how to up the warning level in Dev-C++? I can't find the option for it. Thank you for that suggestion!

I altered the code in the main method, to include the line which you mentioned, and now my code reads as follows:

/****
Assignment3.c
****/

#include <stdio.h>
#include <ctype.h>


void tripleEach(double * array, int size)
{
     int i;
     
     for (i = 0; i < size; i++)
     {
         array[i]=3 * array[i];
     }       
}


void reverse(double * array, int size)
{
    int a=0;
    int swap;

    for(a;a<--size;a++)
    {
        swap=array[a];       
        array[a]=array[size];    
        array[size]=swap;       
    }
}

 
void printArray(double * array, int size)
{
     int i;  

     for (i=0; i < (int) size; i++)
     {
         printf("%g", *(array+i), " "); 
     }
    
}


int main()
{
    int i,n;
    double * numArray;
    int size;
    double num;

    printf("Please enter a number of floating numbers to be entered:\n");
    scanf("%d", &size);

    numArray = (double *) malloc(size * sizeof(*numArray));

    for (i=0; i<size; i++)
    {
       scanf("%lf", &num);  //read in a double entered by user
       *(numArray+i) = num;
    }
 
    printArray(numArray, size);
    printf("\n");
    reverse(numArray, size);
    printf("\n");
    printArray(numArray, size);
    printf("\n");
    tripleEach(numArray, size);
    printf("\n");
    printArray(numArray, size);

    return 0;
}

However, I am having a problem with the code. Now, when I enter the numbers for the array, it just exits out of the program without a warning or anything. Why is it not printing out the output now, and why is it just terminating the program?

Thank you for all of your help, Dave!!

Do you know how to up the warning level in Dev-C++? I can't find the option for it. Thank you for that suggestion!

For gcc, try to add -Wall -ansi -pedantic .

However, I am having a problem with the code. Now, when I enter the numbers for the array, it just exits out of the program without a warning or anything. Why is it not printing out the output now, and why is it just terminating the program?

It's printing, but then the program exits. A short answer is to add a pair of calls to getchar(); before the return 0; in main . More info.

This is still wrong:

printf("%g", *(array+i), " ");

This still needs fixing:

[edit]And to correctly do the swap, you'll want to use a double for the temp (not an int ).

For gcc, try to add -Wall -ansi -pedantic .

It's printing, but then the program exits. A short answer is to add a pair of calls to getchar(); before the return 0; in main . More info.

This is still wrong:

printf("%g", *(array+i), " ");

This still needs fixing:

Okay, let me see... I fixed the last thing you mentioned after posting, so that should be good. For the second to last thing you mentioned, the printf statement, I think the way I have it will work for how I need the output....why do I need to change it? I think its doing what I want, and putting a space between each inputted value. And finally, for the second part of what you said, about the programming printing then exiting, I am confused about what to do. I looked at the link you posted, but it didn't really help my confusion. Do I need to add getchar(); twice? What do I put in the parentheses?

Thank you so much for helping me get this to work, Dave!

Okay, I have made some changes to incorporate the things you mentioned, Dave, and here is what I have so far:

/****
Assignment3.c
****/

#include <stdio.h>
#include <ctype.h>


void tripleEach(double * array, int size)
{
     int i;
     
     for (i = 0; i < size; i++)
     {
         array[i]=3 * array[i];
     }       
}


void reverse(double * array, int size)
{
    int a=0;
    double swap;

    for(a;a<--size;a++)
    {
        swap=array[a];       
        array[a]=array[size];    
        array[size]=swap;       
    }
}

 
void printArray(double * array, int size)
{
     int i;  

     for (i=0; i < (int) size; i++)
     {
         printf("%g ", *(array+i));
     }
     putchar('\n');
    
}


int main()
{
    int i,n;
    double * numArray;
    int size;
    double num;

    printf("Please enter a number of floating numbers to be entered:\n");
    scanf("%d", &size);

    numArray = (double *) malloc(size * sizeof(numArray));

    for (i=0; i<size; i++)
    {
       scanf("%lf", &num);  //read in a double entered by user
       *(numArray+i) = num;
    }
 
    printArray(numArray, size);
    
    reverse(numArray, size);
    printArray(numArray, size);
    
    tripleEach(numArray, size);
    printArray(numArray, size);

    getchar();
    getchar();

    return 0;
}

And it is working perfectly! Except it is still doing one thing that is kind of weird. When I go to close the program, it gives me that same error and then terminates the program. Is that an issue with my program, or my computer, or something else? It may not really matter, but I'm just curious as to why it is doing this.

I think I am ready to turn in my code! Thank you so much for helping me get this working, Dave.

Okay, I have made some changes to incorporate the things you mentioned, Dave, and here is what I have so far:

numArray = (double *) malloc(size * sizeof(numArray));

I missed it when this was introduced. There are a few related things that are still missing.

You should check that malloc succeeds and free(numArray); when you're done with it. The prototypes for these are in #include <stdlib.h> .

I would write it like this:

numArray = malloc(size * sizeof *numArray);

My preference is to avoid the cast, but the * was important.

Except it is still doing one thing that is kind of weird. When I go to close the program, it gives me that same error and then terminates the program. Is that an issue with my program, or my computer, or something else? It may not really matter, but I'm just curious as to why it is doing this.

How is it that you're terminating the program? Just by pressing the Return/Enter key once, right?

Okay, I will change those things. Thank you so much for all of the help!

For some reason, I was exiting the program by closing out of the window (clicking on the x-button), and not by just pressing enter. Of course, it works perfectly when enter is pressed. :-)

Thank you again, Dave!

I am learning from your discussion. Thank you all

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.