1

I want to make a program that uses a function I created where it swaps all the elements of an array X (that has the length of N) with some number K, only if that element is greater than K. Where am I going wrong here?

#include <stdio.h>
#include <stdlib.h>

int swap_K(int *, int);
int main()
{
    int N,i,K;
    printf("Enter N: ");
    scanf("%d",&N);
    printf("Enter K: ");
    scanf("%d",&K);
    int X[N];
    for (i=1; i<=sizeof(X)/sizeof(int); i++){
       printf("Enter %d. element: ",i);
       scanf("%d",&X[i]);
    }
    swap_K(X,K);

    for (i=1; i<=sizeof(X)/sizeof(int); i++){
        printf("%d",X[i]);
    }
}

int swap_K(int *X, int K)
{
    int i;
    for (i=1; i<=sizeof(X)/sizeof(int); i++){
        if (X[i]>K)
            X[i]=K;
    }
    return X;
}
3
  • possible duplicate of C scanf() problem Commented Oct 24, 2014 at 19:01
  • 1
    What specific error are you getting? Also arrays start at 0. Commented Oct 24, 2014 at 19:04
  • Not getting any error, compiles just fine, but it changes only the first element of the array. Also, changed i's from 1 to 0, and "<=" to "<". Thanks. Commented Oct 24, 2014 at 19:09

4 Answers 4

1

In swap_K(int *X, int K), sizeof(X) is sizeof(int *), not the size of the array.

In C, a pointer is not really the same as an array.

To fix it, use N instead of sizeof(X)/sizeof(int) everywhere, esp. inside swap_K().

Sign up to request clarification or add additional context in comments.

2 Comments

It only changes the first element of the array.
Esp. use N inside swap_K().
0

1) Arrays start with index 0.

2) In your main function you don't need to use sizeof(X)/sizeof(int) in for loop as you already know it is equal to N.

3) When you pass the array to the function, you are sending the base address of the array which decays into pointer, so in swap_K function, sizeof(X) will return sizeof(int) which is 4(generally).

To overcome this you should send the size of your array from main function. For example: swap_K(X,K,N);

4) You don't need to return X from swap_K as you are sending the base address of X from main function.

For example:


#include <stdio.h>
#include <stdlib.h>

int swap_K(int *, int, int);
int main()
{
    int N,i,K;
    printf("Enter N: ");
    scanf("%d",&N);
    printf("Enter K: ");
    scanf("%d",&K);
    int X[N];
    for (i=0; i<N; i++)
    {
       printf("Enter %d. element: ",i);
       scanf("%d",&X[i]);
    }
    swap_K(X,K,N);
    for (i=0; i<N; i++)
    {
        printf("%d",X[i]);
    }
}

int swap_K(int *X, int K,int N)
{
    int i;
    for (i=0; i<N; i++)
    {
        if (X[i]>K)
            X[i]=K;
    }
   //return X;  //This is not required
}

1 Comment

I'd give everyone a vote up but I need reputation or smth, I don't know, made the account here especially for this problem. Thanks a lot mate!
0

Your loop is incorrect

for (i=1; i<=sizeof(X)/sizeof(int); i++)

It should be

for (i=0; i<N; i++)

Comments

0

There are several problems with the code posted:

  1. arrays in C are 0-indexed, so the for loops should ALWAYS iterate from 0 to N - 1. Iterating past N is a buffer overflow
  2. the pointer to the array is just the pointer to the first element of the array. The swap function can't know if the pointer passed to it is part of an array or a single value. With this in mind it will need to take another argument which tells what is the size of the passed in array as pointer. Iteration inside the loop will use that value instead of sizeof(X) / sizeof(int) = 1
  3. you're defining X as a variable sized array which is allocated entirely on the stack. Introducing a reasonably large N will crash your program. It would be better to allocate the array in the heap if you don't know what the size of the input will be.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.