Regarding array operations using pointers.....

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • abhinuke
    New Member
    • May 2010
    • 23

    #1

    Regarding array operations using pointers.....

    I am trying to print prime numbers from 1 to 100. I am passing an array to a function and then zeroing all the elements which are multiples of 2,3,5. When I compile the code,I am getting all the memory addresses as an output. Here is the code that I have written.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define size 100
    int main()
    {
        int i,a[size];
        int prime(int *,int);
    
        for(i=1;i<size+1;i++)
        {
            a[i]=i;
        }
        printf("Before procedure the array is\n");
        for(i=1;i<size+1;i++)
        {
    
            printf("%d\n",a[i]);
        }
        prime(&a[0],size);
     return 0;
    }
    int prime(int *ptr,int j)
    {
        int k;
        ptr++;
        for(k=1;k<j+1;k++)
        {
            if(*ptr==2||*ptr==3||*ptr==5)
            {
                break;
            }
            else
            {
                if(*ptr%2==0||*ptr%3==0||*ptr%5==0)
                {
                    *ptr=0;
                }
            }
            ptr++;
        }
    
    ptr=ptr-99;
        printf("Array after removal\n");
    
        for(k=1;k<j+1;k++)
        {
            if(*ptr==0)
            {
                break;
            }
            else
            {
                printf("%d\n",*ptr);
    
            }
            ptr++;
        }
    }
    Further can I anyone of the moderators enlighten me on pointer arithmetic in context of arrays and functions. I have read arrays revealed article but it didn't give me much information.
    Last edited by Niheel; Jun 1 '10, 12:47 PM. Reason: punctuation fixed
  • mayankarpit
    New Member
    • Jun 2010
    • 13

    #2
    He is not printing adrress. infact he is printing garbage values. this is happening bcoz of break stament at line 30. As soon as *ptr == 2 u break from for loop and do ptr = ptr - 99. Bcoz of this ptr starts pointing some garbage location so when loop stars at line 45 it start with printing garbage values

    Comment

    • abhinuke
      New Member
      • May 2010
      • 23

      #3
      Thanks.Worked fine.I guess I'll have to revise break keyword again.
      Had to manipulate some &&,|| operators.

      Comment

      • swatib2k101
        New Member
        • Dec 2009
        • 4

        #4
        Call a pointer string via other pointer

        I have one pointer to string ,
        char *str[4];
        i am calling str[1] in a func say make func()
        How to do it.Seg fault is coming.
        char *str[4];
        makefunc(&str);

        char makefunc(char *pr[])
        How to do it?Please help
        Basically I want to call this str[1] in other function and prints its value.

        Comment

        • akshayshukla
          New Member
          • Jun 2010
          • 1

          #5
          Call a pointer string via other pointer

          You can call function like this.
          makefunc(&pr[1]));

          and define function like this.
          char makefunc(char *pr)
          {
          printf("Passed value is %d", *ptr);
          }

          Comment

          • mayankarpit
            New Member
            • Jun 2010
            • 13

            #6
            Solution suggested by akshay will not solve the segv.Yo have declared a array of char. pointer & before paasing to function you never initialsed it.

            Comment

            • akinidu
              New Member
              • May 2010
              • 7

              #7
              Originally posted by akshayshukla
              You can call function like this.
              makefunc(&pr[1]));

              and define function like this.
              char makefunc(char *pr)
              {
              printf("Passed value is %d", *ptr);
              }
              WHy dont u post the revised code tooo

              Comment

              Working...