Like Tree1Likes
  • 1 Post By Salem

segmentation fault (core dumped) something to do with pointer to struct

This is a discussion on segmentation fault (core dumped) something to do with pointer to struct within the C Programming forums, part of the General Programming Boards category; Code: 1 #include <stdio.h> 2 #include <stdlib.h> 3 4 struct student 5 { 6 char grade; 7 char *last_name; 8 ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    3

    segmentation fault (core dumped) something to do with pointer to struct

    Code:
      1 #include <stdio.h>
      2 #include <stdlib.h>
      3
      4 struct student
      5 {
      6    char grade;
      7    char *last_name;
      8    int student_id;
      9 };
     10
     11 typedef struct student student;
     12
     13 int main()
     14 {
     15 student tmp, *p;
     16 p=&tmp;
     17 /* alternatives:
     18    student tmp, *p=&tmp
     19    asterisk key can only be used in
     20       pointer declaration
     21       pointer dereferencing
     22       when assigning value(memory address), don't use asterisk
     23 */
     24 printf("adaf\n");
     25 tmp.grade='A';
     26 printf("hahaha\n");
     27 tmp.last_name='Phan';
     28 printf("adfaasfafaf\n");
     29 tmp.student_id=123;
     30 printf("haha");
     31 printf("tmp %c %s %d\n", tmp.grade, tmp.last_name, tmp.student_id);
     32 printf("p %c %s %d\n", p->grade, p->last_name, p->student_id);
     33 printf("\n********************************************\n\n");
     34 printf("%d %d\n", (*p).student_id, p->student_id);
     35 printf(" %s %s\n", * p->last_name+1, (*(p->last_name))+1);
     36 printf(" %s %s\n", *(p->last_name+2), (p->last_name)[2]);
     37
     38 return EXIT_SUCCESS;
     39 }
    I think there is some problem with line 29, can somebody give me a hint?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,338
    If line 27 isn't getting any warnings from your compiler, then you need a better compiler.

    If it is warning you - PAY ATTENTION!
    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
    Registered User
    Join Date
    Aug 2011
    Posts
    3

    more problem

    thank u
    my mistake
    but now I got Segmentation fault at line 35 and 36.
    can u give me a hint?

  4. #4
    Maz
    Maz is offline
    Registered User Maz's Avatar
    Join Date
    Nov 2005
    Location
    Finland
    Posts
    194
    How did you change the line 27 Salem pointed to you?

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,338
    Are you paying attention to all the warnings yet?

    $ gcc -Wall foo.c
    foo.c:27:15: warning: multi-character character constant
    foo.c: In function ‘main’:
    foo.c:27: warning: assignment makes pointer from integer without a cast
    foo.c:35: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘int’
    foo.c:35: warning: format ‘%s’ expects type ‘char *’, but argument 3 has type ‘int’
    foo.c:36: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘int’
    foo.c:36: warning: format ‘%s’ expects type ‘char *’, but argument 3 has type ‘int’
    stahta01 likes this.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault (core dumped)????
    By yosipoa in forum C++ Programming
    Replies: 2
    Last Post: 07-20-2011, 01:18 PM
  2. Replies: 7
    Last Post: 03-17-2011, 10:55 AM
  3. Replies: 3
    Last Post: 06-05-2010, 02:58 AM
  4. Segmentation fault, core dumped
    By dweenigma in forum C Programming
    Replies: 2
    Last Post: 05-21-2007, 03:50 PM
  5. Segmentation fault (core dumped)
    By JYSN in forum C Programming
    Replies: 1
    Last Post: 02-21-2002, 02:24 AM

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