Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use same name twice for a variable and for a pointer(c programming)

#include <stdio.h>

void swap(int *i, int *j)
{
    int t;
    t = *i;
    *i = *j;
    *j = t;
}

void main()
{
    int i,j;
    i=5;
    j=10;
    printf("%d %d\n",i,j);
    swap(&i,&j);
    printf("%d %d\n",i,j);

}

Will there be any problem with this small programme which swaps two variables because i,j inside the function work as pointers and inside main contain numbers? should i have used another letters, e.g. a,b instead of i,j inside main?

like image 698
user3642803 Avatar asked Jul 29 '26 12:07

user3642803


1 Answers

Variables are local to the scope they are defined, so no this should not be a problem at all.

like image 76
Mark Nunberg Avatar answered Aug 01 '26 03:08

Mark Nunberg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!