×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

C problem struct and string

C problem struct and string

C problem struct and string

(OP)
Hi guys,

This is my first post and I'm hoping the experts here can help me solve my problem.

- I'm trying to create a linked list in C and the codes look like this:

#include "stdio.h"
#include <stdlib.h>
#include <string>
using namespace std;

struct dir_list
{
    int level;
    string dir_name;
    struct dir_list * head;
    struct dir_list * tail;
};

typedef struct dir_list * dir;

void main()
{
    dir curr;

    curr=(dir)malloc(sizeof(string)+sizeof(dir));
    curr->level=0;
    curr->dir_name="test ";
    curr->head=NULL;
    curr->tail=NULL;
}

The problem with this code happens at the string assignment curr->dir_name="test "; and the executable will stop responding once run. I suspect the problem could be caused by the size of malloc but the result stays the same after I increased that number.

Can anyone explain to me what the problem is?

Greatly appreciated!

RE: C problem struct and string

try this

CODE

#include "stdio.h"
#include <stdlib.h>
#include <string>
using namespace std;

struct dir_list
{
    int level;
    string dir_name;
    struct dir_list * head;
    struct dir_list * tail;
};

typedef struct dir_list dir;

int main()
{
    dir *curr;

    curr=(dir *)malloc(sizeof(dir));
    curr->level=0;
    curr->dir_name="test ";
    curr->head=NULL;
    curr->tail=NULL;
    return 0;
}

RE: C problem struct and string

(OP)
Same :(
if I comment out the string assignment no error will pop-out.

RE: C problem struct and string

It's strange, when I compiled your original source and tried it to run then the executable stopped to respond as you said, but now when I take this source /I added only one printf() line at the end/
lst.cpp

CODE

#include "stdio.h"
#include <stdlib.h>
#include <string>
using namespace std;

struct dir_list
{
    int level;
    string dir_name;
    struct dir_list * head;
    struct dir_list * tail;
};

typedef struct dir_list dir;

int main()
{
    dir *curr;

    curr=(dir *)malloc(sizeof(dir));
    curr->level=0;
    curr->dir_name="test ";
    curr->head=NULL;
    curr->tail=NULL;
    printf("that was ...");
    return 0;
}
and I compile and run it, it runs to end without stopping to respond - look:

CODE

$ g++ lst.cpp -o lst

$ lst
that was ...
You compiled probably the old source once again.
What compiler are you using?

 

RE: C problem struct and string

(OP)
I'm using Visual C++ compiler.
I guess I should try other compilers like yours and see the difference.

Anyway thanks for your time mikrom!

RE: C problem struct and string

I tried it in Visual C++ as win32 console application.
These approaches seems to work:
lst01.cpp

CODE

// lst.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "stdio.h"
#include <stdlib.h>
#include <string>
using namespace std;

struct dir_list
{
    int level;
    char dir_name[80];
    struct dir_list * head;
    struct dir_list * tail;
};

typedef struct dir_list dir;

int main(int *argv, char **argc)
{
    dir *curr;

    curr=(dir *)malloc(sizeof(dir));
    curr->level=0;
    strcpy(curr->dir_name,"test");
    curr->head=NULL;
    curr->tail=NULL;
    printf("that was ...\n");
    return 0;
}
or
lst02.cpp

CODE

// lst.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "stdio.h"
#include <stdlib.h>
#include <string>
using namespace std;

struct dir_list
{
    int level;
    string *dir_name;
    struct dir_list * head;
    struct dir_list * tail;
};

typedef struct dir_list dir;

int main(int *argv, char **argc)
{
    dir *curr;

    curr=(dir *)malloc(sizeof(dir));
    curr->level=0;
    curr->dir_name = new string("test");
    curr->head=NULL;
    curr->tail=NULL;
    printf("that was ...\n");
    return 0;
}

For explanation read this
http://www.cplusplus.com/forum/general/3327/

RE: C problem struct and string

Umm...  Is this supposed to be a C assignment or a C++ assignment?  This is mostly C++ code, but you posted in the C forum.
If it's C++, then get rid of all the malloc()/Free() calls and replace them with new/delete, replace printf() with cout, replace strcpy() with the proper std::string member functions, and use the proper C++ versions of the C headers (i.e. <cstdlib> instead of <stdlib.h>)... although once you get rid of malloc(), you won't need <cstdlib> anymore anyways.
If it's supposed to be C, then get rid of all the C++ stuff.

RE: C problem struct and string

(OP)
it's supposed to be C assignment but i had trouble differentiating them until yesterday.
My problem is now solved and thanks for all the helps!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close