I have a problem with scanf() for string pointer as member of a struct.
No error/warning to compile, but just did not work with "Segment fault!" error.
I believe the problem is with Line 7 and Line 25. If Line 8 is used, everything is fine. I am aware Line 7 does not allocate any memory for studentName, but Line 8 does. How do I solve this problem? Googled for a while, seems a common problem, but I'm still unclear with the right solution. Can anyone please explain it? Thanks a lot!
char *studentName; is a pointer to a string. What does it point to? Absolutely nothing and nowhere in particular. It might point to invalid memory and crash immediately. It might point to somewhere in your own stack frame, causing it to crash later in fascinating nondeterministic ways.
The correct solution would be to either
1) Use line 8
2) Point it to valid memory with a line like list[x].studentName=malloc(20);
Last edited by Corona688; 11-14-2013 at 12:16 PM..
I had thought Line 21 covers the allocation of studentName.
I tried putting this line after Line 25, compiled fine, but did not work. Segment fault! again.
I am trying to use pointer. Where should I put this line for the correct way, not only to make the program run?
Thanks a lot again.
Thanks Carlo! It worked out very well! How stupid I was to put that after Line 25!!!
One more question about free(list[x].studentName). As this variable is within the loop for memory allcation, and will be used for later print. How do I free it in this case? I am confused because of the loop and as member of the struct, which is a array pointer. Handling malloc() is my worst part with C!
Thank you again!
Right now you're not freeing anything, which is... kind of okay... the same way its okay, if sloppy, to not close open files. It all goes away when the program ends anyway. But if you were doing that 10,000 times in a loop you'd be using 10,000 times as much memory which could definitely be a Bad Thing.
How to free it? As soon as you don't need any of that array anymore, do this:
Thanks Corona688!
I tried putting your lines before return 0, it was compiled fine and ran. But after printing it gave many errors:
To fully understand what is happening here, I tried two ways to free the memory. One is within the printf() loop I freed list[x].studentName at Line 35. My understanding is: Once it has been printed, no needed any more and, free it. Then Line 38 free(list) for the struct;
The other is by putting a separate loop Line 37 to free(list[x].studentName). Then the same Line 38 free(list) as list is the struct;
Both had the same problem. Not sure Line 35 is a correct way.
Is Line 35 a correct way to do it?
Is Line 37 the only way to do it when handling 10~100 millions rows?
But both had error. What did I miss? Thanks a lot!
Hello, I am trying to reverse complement one string and reverse another (NO complement!), both with pointer. My code compiled without error, but did not do the job I wanted.
#include <stdio.h>
#include <stdlib.h>
#include <zlib.h>
#include "kseq.h"
// STEP 1: declare the type of file... (5 Replies)
I'm newbie learner. My all friend use windows just only me use linux.
so i can't solve any problem by myself. i need a solution.
how can i use pointer as a string.
#include<string.h>
#include<stdio.h>
int main()
{
char *s='\0';
gets(s);
puts(s);
return 0;
}
This code work on... (6 Replies)
Does anyone know?
int x = 1;
int *p = &++x; //ok !
int *q = &x++; //gives an error :O
why the first pointer is ok but the second is an error? (13 Replies)
hello all,
my question is not about How code can be rewritten, i just wanna know even though i am not using read only memory of C (i have declared str) why this function gives me segfault :wall:and the other code executes comfortably though both code uses same pointer arithmetic.
... (4 Replies)
If one wants to get a start address of a array or a string or a block of memory via a function, there are at least two methods to achieve it:
(1) one is to pass a pointer-to-pointer parameter, like:
int my_malloc(int size, char **pmem)
{
*pmem=(char *)malloc(size);
if(*pmem==NULL)... (11 Replies)
i am having a string like
" X1 " ---> string lenght is 30
I have stored this to a chararry . ref
so here ref = " X1 "
now i trim the left space by my function . Si the string now becomes
"X1 " ---> string lenght is 15... (3 Replies)
i have a function:
char *pcCityIdToCountryName(ADMIN_DB_DATA *pstHEader, unit uiCityID)
this returns a pointer to CountryName if cityId is given.
to retrieve countryname i give:
char *CountryName;
CountryName = pcCityIdToCountryName(..................);
but when i compile it is giving :... (5 Replies)
I have a problem reading characters from keyboard with the scanf function.
Here there is a little piece of code:
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
/* The last 3 libraries are included because in the real program I use some... (4 Replies)