Thread: string(pointer to function)

  1. #1
    Registered User
    Join Date
    Oct 2006
    Location
    New York
    Posts
    124

    string(pointer to function)

    Code:
    #include <stdio.h>
    
    void split_name(char  *a[], char  *b[], char  *c[]);
    
    int main(void)
    {
        static char  *Name[2]={"Justin","Barnes"};
        char  *firstname[1];
        char  *secondname[1];
        
        split_name(Name,firstname,secondname);
    }
    
    void split_name(char  *a[], char  *b[], char  *c[])
    {
       int i=0;
       
                    do
                    {
                         b[1] = a[i];
                             ++i;
                          c[1] = a[i];
                    
                    }while(i>0);
        printf("%s %s",*b,*c);
      getchar();
    }
    My question is sould i use a <- to take the valuable from a[i] to b and c .but then i get an error because of 1 and when i leave it like this i get an compile error soo im wondering how could i take the teo names from array a to b and c..?

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    I'm sorry there are just so many things that are *way* off. I suggest you to go back learning arrays and then pointers because there's absolutely nothing right in your code.

  3. #3
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Please try to keep your replies helpful and considerate.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    903
    I mean.. geez even if we tell him what to change, he won't understand why and he'll make the same mistakes over and over again. Obviously, there's something that's not been understood there.

    Edit: Besides, I think it would be more discouraging than anything to point him all the mistakes.
    Last edited by Desolation; 11-29-2006 at 08:35 PM.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    For the start - what this loop should do:
    Code:
    int i=0;
    do
    {
       //code
       ++i;
    }while(i>0);
    ?
    it runs infinite (actually till the overflow occurs and i is back to 0)
    Do you mean it? Don't think so...
    Start fixing simple things
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Wait, is all you want to do to have firstname = "Justin", secondname = "Barnes"?

    Code:
    #include <iostream>
    
    int main(void)
    {
    
        static char  *Name[2]={"Justin","Barnes"};
        char  *firstname = Name[0];  // "Justin"
        char  *secondname = Name[1]; // "Barnes"
    
        std::cout << firstname << " " << secondname << std::endl;
        
    }
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  7. #7
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Q: How can I use statically- and dynamically-allocated multidimensional arrays interchangeably when passing them to functions?

    http://c-faq.com/aryptr/ary2dfunc3.html

  8. #8
    Registered User
    Join Date
    Nov 2006
    Posts
    65
    Code:
    static char  *Name[2]={"Justin","Barnes"};
    correct if I am wrong, but from what I have read Name is a pointer yet the array is allso called Name? The array is defined and so is the memory adresses.

  9. #9
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Name is a pointer
    yes
    the array is allso called Name
    yes
    The array is defined
    Yes
    and so is the memory adresses.
    here I'm stuck. What do you mean?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  10. #10
    Registered User
    Join Date
    Nov 2006
    Posts
    65
    I just donest make sense to do this, I cant rap my head around why you would do this. What is the purpose of the array with a pointer when its pointing to the same location that the pointer is also located. In general theory or atleast in my understanding its not logical correct and superfluous. I got the impression from the tut's on this site that an array is like a storage box and that a pointer is another storage box that when the usser opens litteraly points/brings/quarrys/ and brings you to the memory location of the array. From there the user can manipulate the array and such. I hope what I am saying makes some sort of sense, oh the fun of programming at 16.

  11. #11
    Registered User
    Join Date
    May 2006
    Posts
    903
    I would compare the pointer to the localization of the box (i.e. aisle number, sub-section..) but you are correct, KoG Metalgod.

  12. #12
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    pointer is another storage box
    No pointer is not a "box", it does not provide a memory to store a value, it just points to some location in the memory... This location could be available or could not be available to the program to store there the data...
    Code:
    char* ptr; //this pointer is not initialized - it cannot be used
    char arr[10]; //this is array, it provides a place for storing 10 bytes, arr can be used as pointer to the first element
    //but it cannot be changed like arr++;
    ptr = arr;//now ptr points to the first element of the array and can be used
    *prt = 'A'; //now we are accessing this first element storing there some data
    ptr++; //now we changing the pointer so it is pointing to the second element of the array
    *ptr='B'; //see above
    arr[2] = 'C';//and now we access the 3rd element of the array using its name
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM