Thread: Function array and pointer

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    1 == 1 tzpb8's Avatar
    Join Date
    Jul 2006
    Posts
    36

    Function array and pointer

    I can only get the first element of an array to work when passed to another function.
    If i try to print out the next element of that array in that function i dont get the result i expected (the strings has 1 letter fewer from the begining when i write e.g: array+i).
    Can someone please tell me what i have done wrong or what is missing?
    The problem is apparent when you see the result of the printf() function, which is in the function roa() and that's where the problem lies i suspect.

    Code:
    /*Pointer array test.*/
    #include <stdio.h>
    void blank(void);
    void roa(char *a,char *b,int *c);
    
    int main(void){
    int i;
    int age[3] = {20,21,18};
    char first[12] = "Tree";
    char first1[12] = "Water";
    char first2[12] = "Dirt";
    char last[12] = "Strotter";
    char last1[12] = "Bridge";
    char last2[12] = "Stone";
    char *given[3];
    char *family[3];
    *given = first;
    *(given+1) = first1;
    *(given+2) = first2;
    *family = last;
    *(family+1) = last1;
    *(family+2) = last2;
    
    blank();
    printf("Name Lastname Age\n");
    for(i=0;i<3;i++)printf("%s %s %d\n",*(given+i),*(family+i),*(age+i));    
    roa(*given,*family,age);
    return 0;}
    
    void blank(void){
    int i;
    for(i=0;i<80;i++)printf("#");
    for(i=0;i<80;i++)printf("#");
    for(i=0;i<32;i++)printf(" ");printf("Array Pointer Test\n");
    for(i=0;i<80;i++)printf("#");
    for(i=0;i<9;i++)printf("\n");}
    
    void roa(char *a,char *b,int *c){
    int i;
    printf("\nroa()\nName Lastname Age\n");
    for(i=0;i<3;i++)printf("%s %s %d\n",a+i,b+i,*(c+i));}
    Last edited by tzpb8; 07-29-2006 at 05:43 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM