i am beginner in c language. pls can u help me in understanding following statements?

#define MAX 5

void insert ( int *, int pos, int num );

void main()

{

int arr[5];

insert ( arr,1,11);

insert (arr,2,12);

........

......

getch();

}

void insert (int *, int pos,int num)

{

int i;

for (i=MAX-1; i>=pos ; i--)

arr[i]=arr[i-1]

arr[i]=num;

}

i could not understand what is the meaning of "int *". but if "int p" is there i can understand that it p is a variable assign to interger type, but how come '*' be assigned as integer type and what is it actually doing. and what is

insert (arr,1,11) function in the main()

5 Answers

Relevance
  • 1 decade ago
    Favorite Answer

    The intent of this code is to insert a series of integers into an integer array. It inserts them by shifting all the elements of the array up, so that the inserted item goes into the array position that is passed.

    In order to reference an array as a passed parameter, you pass the memory location of the array. "int * arr" would say "the integer array 'arr' which starts at this memory location".

    It looks like there's a bug in the function header for "void insert" since it doesn't list the array name (should say "int * arr" but says "int *").

    Also, it's sloppy to use a constant MAX and not dimension the array to MAX. That's just asking for a system crash later if the dimension of the array gets shorter than MAX. Then you'll be shifting bytes in memory locations that might hold instructions. Yuck.

    • Login to reply the answers
  • Anonymous
    1 decade ago

    int* ---> the star refers that the integer declared is a pointer , pointing to a memory address of an integer type.for instance ,when u declare a normal integer ,lets take an example:

    int p=5;

    here u declared an integer and gave it the value five, which is stored in a memory address inside the computer's memory.int* z;

    when u do z=&p; (z which is a pointer, is pointing at the memory address of where 5 is stored, therefore if u try to cout z, u will not get 5 , but a memory address like 8EF452W or anything garbage which refers to the location of 5 in the memory. as for insert(arr,1,11), it is a normal function to test the program.the insert function is decalred outside the main, and the function prototype just above the main. this function takes an array of pointers,an integer pos, and an integer num),hence if insert(arr,1,11) --> arr is tthe array , 1 is the position , num is the number

    • Login to reply the answers
  • 1 decade ago

    Firstly you should know what the pointer is. And i think you know about array.

    A pointer is a variable that stores memory address rather than value and is denoted by *.

    so a simple pointer is;

    int *a; // means 'a' is pointer variable

    so int * here is a pointer.

    if you simply pass int p then only value is passed and changes are not reflected once the control is back to main.

    here the code sorts the array.

    insert (arr,1,11) => you are passing address of first member of an array arr, so whatever changes you made inside the function is directly changed to memory.

    • Login to reply the answers
  • 1 decade ago

    int p means p is a variable of type int. i think u knw this.

    int *p means p is a pointer variable which points to a variable of type int.

    pointers r special variables which donot store any value but an address of another variable.

    suppose we have a statement:

    int i=5;

    it means i is a variable of type int storing value 5. since variables r stored in memory, they have some address. now suppose i is stored at memory location 2000.

    now, we have a situation something like this:

    value of i = 5

    address of i = 2000.

    now suppose, v have second statement as:

    int *ptr;

    ptr = &i;

    first statement means v have declared a pointer ptr which will store an address of some int variable.

    second statement means v r assigning address of i to pointer ptr.

    "&" is called "address of" operator. it gives us the address of variable.

    now, we have a situation something like this:

    value of i = 5.

    address of i = 2000.

    value of ptr = 2000.

    i think this will help u in understanding pointers.

    now, ur second ques:

    in the declaration part, insert function has 3 arguments: 1 pointer, 2 int variables.

    when calling function insert, v r passing 3 parameters: the array arr, int value 1 and int value 11.

    the base address of arr will b assigned to pointer, pos will get value 1 and num will get value 11.

    • Login to reply the answers
  • How do you think about the answers? You can sign in to vote the answer.
  • 3 years ago

    Pay noticeably careful interest to the algorithms section. on an identical time as documentation is important, and in many circumstances graded as such, in case you comprehend what your instructor and the literature is telling you approximately the thank you to do issues, then something must be common. For an introductory programming direction, the subject to bear in mind is you do not have plenty attitude yet. the college will probable have good motives for picking this or that language, yet you're getting to grasp the language and the self-discipline and it relatively is relatively obtainable that in case you learn forward (specializing in C++) you're able to locate that what you have studied are the incorrect issues. the comparable could be real of your fellow scholars. in this subject count, the Wikipedia is an extremely good source. because of the fact the content cloth is person-generated, it does have some flaws, in spite of the undeniable fact that it has the variety of extensive variety of people, noticeably on laptop-appropriate matters, that as an creation to many matters it relatively is as good because of the fact the websites which cut back their contributions to qualified "specialists". i comprehend there are arguments happening approximately this suitable now, yet we are speaking relating to the Wikipedia whilst it relatively is study critically. you will get your textbooks in August or September. they might desire to be what you like for the specifics of the type you will get interior the direction of the class. Any generic creation to the priority of programming ought to help. If in basic terms with the aid of putting your ideas comfortable.

    • Login to reply the answers
Still have questions? Get your answers by asking now.