problem with looping scanf

This is a discussion on problem with looping scanf within the C Programming forums, part of the General Programming Boards category; I'm trying to fix someones code for them but I've got myself stuck. Code: int main() { float x = ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    137

    problem with looping scanf

    I'm trying to fix someones code for them but I've got myself stuck.
    Code:
    int main()
    {
    	float x = 0, temp=0 , high, low;
    	int num,i,*ptr,sum = 0,mean;
    	char choose;
    
    	printf("This program will ask the user for a list of numbers.\n");
       printf("Then calculate the median or mean average.\n\n");
    	printf("How many numbers are you going to put in?\n:  ");
    	scanf("%d",&num);
    
       ptr = (int*) malloc ( num  * sizeof (int));    //Creates enough room for the array
       if (ptr == NULL) printf("Malloc failed");
       for (i=0;i<=num;i++)	{                   //Fills the array with inputted values
          printf("i = %d\n",i);
          scanf("%d",*(ptr + i)); //PROBLEM HERE
          }
    After I've inputted 3 values ( I should be able to put much more in ) in crashes my dos prompt. Can anyone here see whats wrong?


    This isn't full code by the way, if you are wondering why there are lots of unused variables.
    Last edited by crag2804; 09-12-2002 at 07:43 PM.
    http://uk.geocities.com/ca_chorltonkids

  2. #2
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946

    Re: problem with looping scanf

    Originally posted by crag2804
    I'm trying to fix someones code for them but I've got myself stuck.
    Code:
    int main()
    {
    	float x = 0, temp=0 , high, low;
    	int num,i,*ptr,sum = 0,mean;
    	char choose;
    
    	printf("This program will ask the user for a list of numbers.\n");
       printf("Then calculate the median or mean average.\n\n");
    	printf("How many numbers are you going to put in?\n:  ");
    	scanf("%d",&num);
    
       ptr = (int*) malloc ( num  * sizeof (int));    //Creates enough room for the array
       if (ptr == NULL) printf("Malloc failed");
       for (i=0;i<=num;i++)	{                   //Fills the array with inputted values
          printf("i = %d\n",i);
          scanf("%d",*(ptr + i)); //PROBLEM HERE
          }
    After I've inputted 3 values ( I should be able to put much more in ) in crashes my dos prompt. Can anyone here see whats wrong?


    This isn't full code by the way, if you are wondering why there are lots of unused variables.
    stuff:
    you didnt return a value from main()
    when you allocate space for num ints, your for loop should look like this:
    for (i=0;i<num;i++)
    how much do you know of pointers? i suggest getting a good book on them. scanf expects a pointer to int arg, which is why with a regular int you take the address of it with the & operator. but why are you indirecting in
    scanf("%d",*(ptr + i));
    ? The indirection operator yields an int from a pointer to an int. but scanf expects a pointer, not an int. so you should just do
    scanf("%d", ptr + i);
    hello, internet!

  3. #3
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    also, if your malloc () fails, you print the message that it did, but then continue on to access the invalid pointer.
    hello, internet!

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    137
    no return-its not the full code.
    scanf("%d",*(ptr + i)); I thought was same as
    scanf("%d",ptr[i]);
    Is it?

    Code:
    ptr = (int*) malloc ( num  * sizeof (int));    //Creates enough room for the array
       if (ptr == NULL) printf("Malloc failed");
       for (i=0;i<num ;i++)	{                   //Fills the array with inputted values
          printf("i = %d\n",i);
          scanf("%d",ptr[i]);
          }
    I've recompiled as that but it doesn't help still crashes. it doesn't crash if I put 2 values into the array.
    http://uk.geocities.com/ca_chorltonkids

  5. #5
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by crag2804
    no return-its not the full code.
    scanf("%d",*(ptr + i)); I thought was same as
    scanf("%d",ptr[i]);
    Is it?
    /me taps into final bits of brain power before sleep sets in

    why of course it is. but, both expressions *(ptr + i) and ptr[i] yield an int. scanf wants an int *. this is why when you scanfed for num you didnt do

    scanf("%d",num);

    you did

    scanf("%d",&num);

    Much in the same way, you need to take the address of the other expression to yield an int *. so:

    &ptr[i]

    or

    &*(ptr + i)

    both are valid. but the &* is of course redundant, so that expression is equivalent to

    ptr + i
    hello, internet!

  6. #6
    Registered User Dr. Bebop's Avatar
    Join Date
    Sep 2002
    Posts
    96
    *(ptr + i)
    and
    ptr[i]

    are both the same, but scanf takes a pointer, so if you used the array indexing you would have to use &ptr[i] and if you used pointer notation, which is stupid since array indexing is easier to read, you would have to say either (ptr + i) or &*(ptr + i). Don't forget that you allocated space for 3 integers but your loop goes 4 times. That's an off by one bug, the loop test should be i < num.
    Processing error: Stupidity detected.
    ------------------------------
    Dr. Bebop
    Windows XP Professional Ed.
    Microsoft Visual Studio 6

  7. #7
    Registered User
    Join Date
    Sep 2002
    Posts
    137
    Thanks a lot, I guess that's what you get for coding at 3:08am. Seems so simple now.
    http://uk.geocities.com/ca_chorltonkids

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  2. Problem with RPC and scanf
    By Ricardo_R5 in forum C Programming
    Replies: 11
    Last Post: 01-08-2007, 05:15 PM
  3. scanf problem
    By kiranck007 in forum C Programming
    Replies: 3
    Last Post: 01-30-2006, 08:27 AM
  4. looping problem
    By chris285 in forum C++ Programming
    Replies: 4
    Last Post: 04-22-2005, 11:03 AM
  5. Looping Problem
    By simly01 in forum Windows Programming
    Replies: 1
    Last Post: 06-28-2002, 01:05 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21