Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
C help
#1
How do I combine two strings together. I need the C equivalent of

Code:
return name + " in " + department;

I can't figure out how to do it. Also, I'm getting some warning that I don't quite understand. Where are the two warning coming from (I know the printf, but why?) and what does it mean by Undefined symbols. All of those functions are declared in

Quote:driver.c: In function 'main':
driver.c:24: warning: incompatible implicit declaration of built-in function 'printf'
driver.c:28: warning: incompatible implicit declaration of built-in function 'printf'
/usr/bin/ld: Undefined symbols:
_MajortoString
_StudenttoString
_getMajor
_newmajor
_newstudent
collect2: ld returned 1 exit status


Code:
#include "student.h"    //everything i need is in this header and associated files

int main(int argc, char **argv){
    int i, temp;
    major_t *majors[] = {
        newmajor("Computer Science", "Computer Science"),
        newmajor("Mathematics", "Mathematics"),
        newmajor("Math Ed", "Mathematics"),
        newmajor("CS Grad", "Computer Science"),
        newmajor("Olde Englishe", "English")
        };
    student_t *students[] = {
        newstudent("Joe", 17, majors[0], 3.5f),
        newstudent("Jane", 18, majors[1], 3.8f),
        newstudent("Jack", 17, majors[2], 2.5f),
        newstudent("Jill", 22, majors[3], 4.0f),
        newstudent("John", 24, majors[4], 1.0f)
        };
    if (argc > 0){
        for (i = 0; i < 4; i++){
            if (MajortoString(getMajor(students[i])) == argv[1])
                printf("%s\n", StudenttoString(students[i]));
            }
        }
    else
        printf("%s", "You must enter a Major name at the command line... dumbass!");
}


Grr. I usually don't post homework, but I've spent a lot of time trying to figure this out for myself, but haven't been able to get it. Thanks in advance.

EDIT: I'm using GNU C compiler for OSX, which is UNIX.
f you play a Microsoft CD backwards you can hear demonic voices. The scary part is that if you play it forwards it installs Windows.
Reply
#2
strncat
\__/)
(='.'=) Copy bunny into your signature to
(")_(") help him gain world domination.
Reply
#3
the reason for the warnings regarding printf is because you need to include the headers, like

#include <stdio.h>
#include <string.h>

etc for the functions you want to use.
EVEN MEN OF STEEL RUST.
[Image: chav.gif]
Reply
#4
Did you put them in student.h?
\__/)
(='.'=) Copy bunny into your signature to
(")_(") help him gain world domination.
Reply
#5
both of those headers are in student.h
f you play a Microsoft CD backwards you can hear demonic voices. The scary part is that if you play it forwards it installs Windows.
Reply
#6
Code:
char *mystring;

mystring = strdup (name);
strcat (mystring, " in ");
strcat (mystring, department);

Beware, if you return mystring, you won't be returning a copy of the string but a pointer to it.

Memory taken by mystring has to be manually freed, as well. strdup and strcat allocate memory.
SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
Reply
#7
strcat does not allocate memory; if you don't have a big enough buffer, it will overflow, no questions asked. You have to malloc (or statically reserve) a large enough buffer for the destination string.

The "Undefined symbols" message is coming from ld, the linker. This means that those symbols are defined in a different file (they are declared in student.h). Link your object file with theirs and magic will happen.

EDIT: Also note that 'argc > 0' is probably not what you want. argc is always at least 1, since argv[0] is the program's filename.
Reply
#8
Argh you're so right, I forgot the ol' trick...

I used to make a function...

Code:
void concat (char *string1, char *string2)
{
   realloc (string1, (strlen(string1) + strlen(string2) + 1) * sizeof(char));
   strcat (string1, string2);
}

which took care of it Big Grin
SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
Reply
#9
Quote:The "Undefined symbols" message is coming from ld, the linker. This means that those symbols are defined in a different file (they are declared in student.h). Link your object file with theirs and magic will happen.

Thanks, my professor helped me figure that out. I'm so used to using IDEs that the command line is like a foriegn language.[/quote]
f you play a Microsoft CD backwards you can hear demonic voices. The scary part is that if you play it forwards it installs Windows.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)