Thread: using pointer inside the for loop conditions

  1. #1
    Registered User
    Join Date
    May 2020
    Posts
    3

    using pointer inside the for loop conditions

    #
    Code:
    void modify(int *k)
    {
        *k+=5;
    }
    
    
    int main()
    {
        int i;
        int a[]={3,6,2,5,1};
        int *k=NULL;
        modify(&k);
        for(i=0;i<*(k);++i)
          {
            printf("\n%d",a[i]);
          }
    return 0;
    }

    here is a small code that modifies the value of pointer k and then uses it as a conditioner inside the for loop.here i use *(k) with an aim to dereference the pointer k to the value 5, but it gives a segmentation fault instead if i use k without the derefrence operator inside the for loop
    Code:
    for(i=0;i<k;++i)
    it works well and good. why don't we need to dereference the pointer k inside the loop

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    If the code actually compiles (it doesn't compile on my system) the crash is understandable since you're not passing what you think you are into that function.

    Don't confuse NULL with zero, in C they are not necessarily the same.

    Next since k is already a pointer using the ampersand in the function call is wrong. But when you take it away, you now have a NULL pointer that you try to assign a value to, CRASH.

    Code:
    void modify(int *k)
    {
        *k+=5;
    }
     
     
    int main()
    {
        int i;
        int a[] = {3, 6, 2, 5, 1};
        int k = 0;
    
        modify(&k);
    
        for(i = 0; i < k; ++i)
        {
            printf("\n%d", a[i]);
        }
        return 0;
    }

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Your first problem is a matter of a type mismatch:
    Code:
    void modify(int *k)
    versus:
    Code:
    int *k=NULL;
    modify(&k);
    &k is an int**, which does not match the int* parameter.

    Your second problem is that k is a null pointer. This means that you cannot dereference it. What you probably want to do is to set k to point to something, and then pass k to modify so that what it points to can have 5 added to it. For example:
    Code:
    int *k = a + 1; // k points to a[1]
    modify(k); // a[1] += 5
    However, the fact that you are trying to use k to control the loop over the array a suggests that you actually want k to contain the value of 5 (i.e., the number of elements in the array a). This means that your entire setup is wrong. What you want would then be:
    Code:
    void set_array_size(int *array_size)
    {
        *array_size = 5;
    }
    
    int main(void)
    {
        int i;
        int a[] = {3, 6, 2, 5, 1};
        int k;
        set_array_size(&k);
        for (i = 0; i < k; ++i)
        {
            printf("%d\n", a[i]);
        }
        return 0;
    }
    That said, this is something of a Rube Goldberg machine. Normally, we would just write:
    Code:
    int main(void)
    {
        int a[] = {3, 6, 2, 5, 1};
        const size_t k = sizeof(a) / sizeof(a[0]);
        int i;
        for (i = 0; i < k; ++i)
        {
            printf("%d\n", a[i]);
        }
        return 0;
    }
    Quote Originally Posted by supernova_2706
    why don't we need to dereference the pointer k inside the loop
    You do, since k is a pointer. What's happening is that since k is a pointer, either i is getting implicitly converted to a pointer to int or k is getting implicitly converted to int (sorry, I don't remember the conversion rules for this unusual case), and the compiler happened to accept this (probably reluctantly, as you should be getting a warning). It doesn't mean your code is correct, even if it happens to "work". As you passed the address of k to modify() earlier, what might have happened is that your pointer to int was reinterpreted as an int with the value of 0, and then had 5 added to it. So, when you convert back again from a pointer to an int, or when you're comparing an int converted to a pointer, you might get a result similiar to just comparing say, an integer with the value 5. But this convoluted mess is not the way to go.

    Oh, and you should compile at high warning levels and pay attention to the warnings.
    Last edited by laserlight; 05-04-2020 at 03:48 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. While loop with multiple conditions.
    By Tmadden49 in forum C Programming
    Replies: 8
    Last Post: 04-09-2020, 05:16 PM
  2. combined conditions in For loop
    By sjmp in forum C Programming
    Replies: 5
    Last Post: 08-24-2012, 04:35 AM
  3. Pointer becomes NULL inside loop
    By nepper271 in forum C Programming
    Replies: 1
    Last Post: 06-06-2010, 11:13 PM
  4. Multiple conditions for the while loop?
    By Olidivera in forum C++ Programming
    Replies: 6
    Last Post: 04-24-2005, 03:47 AM
  5. While Loop Conditions?
    By ted_smith in forum C Programming
    Replies: 8
    Last Post: 01-17-2005, 10:20 PM

Tags for this Thread