cont...

This is a discussion on cont... within the C++ Programming forums, part of the General Programming Boards category; This is a continuation of my previous question. So far this what I have...the for loop finds the length of ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    5

    Question cont...

    This is a continuation of my previous question.
    So far this what I have...the for loop finds the length of the string but I don't understand how to write the function that will pass the array.
    #include <stdio.h>
    #include <stdlib.h>
    main()
    {
    int i;
    char a[];
    char b[];

    for ( i = 0; a[i] != '\0'; i++ );
    return i;

  2. #2
    Unregistered
    Guest
    char array1[60];
    char array2[10];
    int i = 6;

    char * insert(char*, char*, int);

    insert(array1, array2, i);

    char * insert(char * Array1, char * Array2, int I)
    {
    //here's one way to actually do it, without shifting contents of array1;

    char array3[80];
    //copy chars for array1 into array3 up to inex i
    //copy chars for array2 up to but not includeing '\0' into array3
    //copy remaining chars from array1 into array3;
    //make sure array3 has null terminating char
    //return array3;
    }

  3. #3
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    That won't work as array3 as local scope, it's
    gone after the function returns.
    You have to be careful you can write a
    function like

    char* hello_world()
    {
    char* p = "Hello World";
    return p;
    }

    This works because "Hello World" is stored statically.

    When you pass arrays, the c compiler doesn't do
    a member by member copy, you generally want to
    pass the base address of the array.

    Copying by value can be accomplished by

    struct A {
    int A[30];
    };

    And then
    void f(struct A a);

    I've heard of people having trouble passing large arrays
    by value like this though.

  4. #4
    Unregistered
    Guest
    Nick is right, you can do the shift in array1's char to allow insertion of array2, you can pass three arrays instead of 2, or you could try something like this:

    char array4[80]
    strcpy(array4, insert(array1, array2, i))

    which should work like returning by value an int local to the called function and assigning it to an int in the calling function, although can't say I've done this lately to say categorically that it will work.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cont of IPC using PIPE
    By BMathis in forum C Programming
    Replies: 1
    Last Post: 03-15-2009, 05:16 PM
  2. Maze Solving Cont....PLZ
    By jonnybgood in forum C Programming
    Replies: 30
    Last Post: 11-11-2005, 03:00 AM
  3. cont' undeclared
    By HotMustardGas in forum C++ Programming
    Replies: 5
    Last Post: 06-12-2005, 04:13 PM
  4. Homework Cont'
    By Kurious in forum C++ Programming
    Replies: 4
    Last Post: 02-01-2003, 04:04 PM
  5. Cont
    By kermi3 in forum C Programming
    Replies: 0
    Last Post: 01-13-2003, 06:07 PM

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