Function array and pointer

This is a discussion on Function array and pointer within the C Programming forums, part of the General Programming Boards category; I can only get the first element of an array to work when passed to another function. If i try ...

  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.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,556
    Like so
    Code:
    /*Pointer array test.*/
    #include <stdio.h>
    void blank(void);
    
    /* Look at the arrays you want to pass */
    /* and copy the declarations.  Using meaningful names */
    /* would be good as well */
    void roa(char *given[], char *family[], int age[]);
    
    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[0] = first;
      given[1] = first1;
      given[2] = first2;
      family[0] = 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 *given[], char *family[], int age[])
    {
      int i;
      printf("\nroa()\nName Lastname Age\n");
      for (i = 0; i < 3; i++)
        printf("%s %s %d\n", given[i], family[i], age[i] );
    }
    > *(given+1) = first1;
    There is no difference between what you wrote, and what I wrote, it's all the same to the compiler.
    So since it's an array, treat it like an array and use a subscript.

    Oh, and learn to format code in something like a reasonable fashion.
    This super-compressed eliminate as many spaces as possible doesn't help figure out what is going on, has no measureable consequence on the compiler, and absolutely no impact on the generated code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    1 == 1 tzpb8's Avatar
    Join Date
    Jul 2006
    Posts
    36
    Great thank you, it works

    How is this formating? (it was really hard for me to split the for loops and printf functions to seperate lines )
    Code:
    /*Pointer array test.*/
    #include <stdio.h>
    void blank(void);
    void roa(char *given[],char *family[],int age[]);
    
    int main(void){
    int i;
    int age[3] = {20,21,18};
    char first[] = "Tree";
    char first1[] = "Water";
    char first2[] = "Dirt";
    char last[] = "Strotter";
    char last1[] = "Bridge";
    char last2[] = "Stone";
    char *given[3];
    char *family[3];
    
    given[0] = first;
    given[1] = first1;
    given[2] = first2;
    family[0] = 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 *given[],char *family[],int age[]){
    int i;
    printf("\nroa()\nName Lastname Age\n");
    for(i=0;i<3;i++)
     printf("%s %s %d\n",given[i],family[i],age[i]);}

  4. #4
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    You should use this:
    Code:
    void roa(char *a[3],char *b[3],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));
    }
    Or
    Code:
    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));
    }
    Second code only works for char. Else you need too specify ptr offset yourself.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  5. #5
    1 == 1 tzpb8's Avatar
    Join Date
    Jul 2006
    Posts
    36
    @siavoshkc: that works also, cool now i think i know how to do it both ways.
    Though you forgot to edit the arrays passed to not be pointers to pointer arrays but to just pointer arrays like this:
    Old:
    Code:
    roa(*given,*family,age);
    New:
    Code:
    roa(given,family,age);

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,556
    > it was really hard for me to split the for loops and printf functions to seperate lines
    What sort of editor are you using?
    How hard can it be to press return at the point you want, and indent the code to suit?

    Besides, I would prefer this
    Code:
      for(i=0;i<3;i++) {
        printf("%s %s %d\n",given[i], family[i], age[i] );
      }
    Always use braces even when they're apparently optional.
    No end of newbies come unstuck when they forget the single-statement rule and the code does something completely different, when they try and add debug statements for example.

    Example
    Code:
    for ( i = 0 ; i < 10 ; i++ )
      printf( "%d\n" );
    
    for ( i = 0 ; i < 10 ; i++ )
      printf( "Inside loop\n" );  /* apparently to debug the loop... */
      printf( "%d\n" );
    Which, despite it's indentation, will only be executed once when the loop exits.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    pointer to array of pointer will be automaticaly pointer to pointer to pointer when passed to functions.
    this is good to be read. Though it is C++.
    Last edited by siavoshkc; 07-29-2006 at 06:48 AM.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  8. #8
    1 == 1 tzpb8's Avatar
    Join Date
    Jul 2006
    Posts
    36
    @Salem: I use the editor that comes with Dev-C++ 4.9.9.2.
    It is not the editor.. its just my taste, see i even forgot to add line breaks within the blank() function for printing out an almost clear screen with just some intro text at top.
    Code:
    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");}

  9. #9
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Well, just so you know, we, too, have different tastes on how to go about things. Besides, what's so hard about going to Tools > Editor Options and checking "Auto Indent"?
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,556
    Well prepare yourself for lots of frustration on your part, constant nagging from everyone else to adopt a more reasonable style, then eventually people just refusing to even look at such horrible code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  11. #11
    1 == 1 tzpb8's Avatar
    Join Date
    Jul 2006
    Posts
    36
    I didn't mean to cause frustration sorry if i did, so if i write my loops on one line and statements (however small or large they might be) on another line and write like i did in#4, i would write conventional?

    I wrote an example program:
    Code:
    #include <stdio.h>
    void blank(void);
    
    int main(void)
    {
      int i;
        
      char stringy[] = "Conventional";
      char *array[1];
      array[0] = stringy;
        
      blank();  
        
      for(i=0;i<13;i++)
        printf("%c",stringy[i]);
        
      array[0] = "Style?";
      printf("\n%s",array[0]);
      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<20;i++)
        printf(" ");
      printf("String + (Conventional Style) Test\n");
      for(i=0;i<80;i++)
        printf("#");
      for(i=0;i<18;i++)
        printf("\n");
    }

  12. #12
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    tzpb8: I would think that a structure would really lend itself to what you are doing. Something like this?
    Code:
    #include <stdio.h>
    
    struct record
    {
       char first[12];
       char last[12];
       int  age;
    };
    
    void record_print(struct record *rec, size_t size)
    {
       printf("%-*s %-*s %s\n", (int)sizeof rec->first, "First Name", 
              (int)sizeof rec->last, "Last Name", "Age");
       for ( ; size--; ++rec )
       {
          printf("%-*s %-*s %d\n", (int)sizeof rec->first, rec->first, 
                 (int)sizeof rec->last, rec->last, rec->age);
       }
    }
    
    int main(void)
    {
       struct record myrecord[] =
       {
          { "Tree",  "Strotter", 20 },
          { "Water", "Bridge",   21 },
          { "Dirt",  "Stone",    18 },
       };
       record_print(myrecord, sizeof myrecord / sizeof *myrecord);
       return 0;
    }
    
    /* my output
    First Name   Last Name    Age
    Tree         Strotter     20
    Water        Bridge       21
    Dirt         Stone        18
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  13. #13
    1 == 1 tzpb8's Avatar
    Join Date
    Jul 2006
    Posts
    36
    I wanted to learn pointers and arrays better but now i'm moving on to structers so thanks for the rewrite Dave but sence i cant at variables declaration inside structers assign values, how can i assign a string value to a char array like this?
    Code:
    char array[] = "Some text";
    without defining & assigning value at declaration?


    Oh and here i have a slight problem with prining out as a string.. it prints out the value good but when it is supposed to end it will printout something more, that i dont want, what is it and what can i do about it?

    Code:
    /* Structures */
    #include <stdio.h>
    void blank(void);
    
    int main(void)
    {
      int i;
      
      blank();
      
      struct wall
      {
      char array[];
      char stringPNT[];      
      } roa;
    
      roa.array[0] = 'B';
      roa.array[1] = 'l';
      roa.array[2] = 'u';
      roa.array[3] = 'e';
      
    // though i would like to do it this way instaid...  
    //  roa.array[] = "Blue";
    
      for(i=0;i<4;i++)
        printf("%c",roa.array[i]);
        
      *roa.stringPNT = *roa.array;  
      
      roa.stringPNT[0] = 'P';
      roa.stringPNT[1] = 'i';
      roa.stringPNT[2] = 'n';
      roa.stringPNT[3] = 'k';
      
      printf("\n");
      
      for(i=0;i<4;i++)
        printf("%c",roa.stringPNT[i]);
    
      printf("\n%s",roa.stringPNT);
      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("Structures\n");
      for(i=0;i<80;i++)
        printf("#");
      for(i=0;i<17;i++)
        printf("\n");
    }
    Last edited by tzpb8; 07-31-2006 at 08:08 PM.

  14. #14
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by tzpb8
    I wanted to learn pointers and arrays better but now i'm moving on to structers so thanks for the rewrite Dave but sence i cant at variables declaration inside structers assign values, how can i assign a string value to a char array like this?
    Code:
    char array[] = "Some text";
    without defining & assigning value at declaration?
    I really can't parse what it is you are asking. My best guess is that you are asking for strcpy.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  15. #15
    1 == 1 tzpb8's Avatar
    Join Date
    Jul 2006
    Posts
    36
    Woaw, it works beautifully, strcpy is exactly what i asked for, thanks again Dave your great
    Last edited by tzpb8; 07-31-2006 at 08:24 PM.

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, 08: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, 06:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21