Thread: need help with finding an error in a code

  1. #1
    Registered User
    Join Date
    Dec 2018
    Posts
    1

    need help with finding an error in a code

    i dont know what is the problem in this code but after i enter my 5 numbers for the array its crash.

    someone can please explain to me whats going on here but really detailed tnx/.


    Code:
    #define _CRT_SECURE_NO_WARNINGS
    #include<stdio.h>
    #include<stdlib.h>
    #define SIZE 5
    int main()
    {
    	int i;
    	int *p = (int*)calloc(SIZE, sizeof(int));
    	for (i = 0; i<SIZE; ++i)
    	{
    		*p = scanf("%d", p);
    		p++;
    	}
    	for (i = 0; i<SIZE; ++i)
    	{
    		printf("%d\t", p[i]);
    	}
    	free(p);
    	return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,529
    > p++;
    It crashes because you messed around with your pointer.

    > printf("%d\t", p[i]);
    It also crashes because you're now indexing past the end of the array, after all those p++.

    > *p = scanf("%d", p);
    Just do
    scanf("%d", &p[i]);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User Kernelpanic's Avatar
    Join Date
    Sep 2018
    Location
    Berlin
    Posts
    103
    Quote Originally Posted by ofir View Post
    i dont know what is the problem in this code but after i enter my 5 numbers for the array its crash.
    someone can please explain to me whats going on here but really detailed tnx/.

    Code:
    #define _CRT_SECURE_NO_WARNINGS
    #include<stdio.h>
    #include<stdlib.h>
    #define SIZE 5
    int main()
    {
        int i;
        int *p = (int*)calloc(SIZE, sizeof(int));
        for (i = 0; i<SIZE; ++i)
        {
            *p = scanf("%d", p);
            p++;
        }
        for (i = 0; i<SIZE; ++i)
        {
            printf("%d\t", p[i]);
        }
        free(p);
        return 0;
    }
    Why one use pointers even though one do not pointer understand?

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    
    #define SIZE 5
    
    
    int main(void)
    {
      int feld[5];
        int zahl;
        
        printf("\nInput five numbers: \n");
        for (int i = 0; i < SIZE; i++)
        {
            scanf("%3d", &feld[i]);
            if (i == SIZE)
                { break; }
        }    
            
        printf("\n\n");
        for (int i = 0; i< SIZE; i++)
        {
            printf("%3d", feld[i]);
        }    
      return 0;
    }

  4. #4
    Registered User Kernelpanic's Avatar
    Join Date
    Sep 2018
    Location
    Berlin
    Posts
    103
    And this is with color:

    Code:
    #include <windows.h>
    . . .        
        printf("\n\n");
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), fgcolor = 10);
        for (int i = 0; i< SIZE; i++)
        {
            printf("%3d", feld[i]);
        } 
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), fgcolor = 7);    
      return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Finding the error
    By DianaOli in forum C Programming
    Replies: 6
    Last Post: 01-16-2017, 01:30 PM
  2. Trouble finding error in code
    By 856915iss in forum C Programming
    Replies: 13
    Last Post: 12-01-2012, 01:46 PM
  3. Please help me finding the error... :(
    By fredsilvester93 in forum C Programming
    Replies: 4
    Last Post: 01-04-2012, 01:15 AM
  4. need help in finding the error in my code
    By zeybek258388 in forum C Programming
    Replies: 5
    Last Post: 12-23-2011, 04:38 PM
  5. Need help finding error
    By chorney in forum C++ Programming
    Replies: 13
    Last Post: 01-15-2007, 12:16 AM

Tags for this Thread