I am trying to program a code with memory allocation. I feel like the code is right and it compiles properly, however im hit with a segmentation fault when I try and run it. The following is my code and any help will be appreciated. - #include <stdio.h>
-
#include <stdlib.h>
-
#include <string.h>
-
#include <malloc.h>
-
-
void pound(int argc, char*str)
-
{
-
int i;
-
i = 0;
-
do {
-
printf("Input Parameter= ##%s##\n",str);
-
i = i + 1;
-
} while (i < argc);
-
}
-
-
int
-
main(int argc, char**argv)
-
{
-
char *str;
-
unsigned num;
-
-
printf("Size of array = ");
-
scanf("%d", num);
-
str = (char *)malloc((size_t)num + 1);
-
if (str == NULL)
-
{
-
printf("Can't allocate memory!");
-
exit(0);
-
}
-
printf("Allocated array!");
-
free(str);
-
-
if (argc < 3) {
-
printf("Error: You need to pass in atleast one argument.\n");
-
exit(-1);
-
}
-
strncpy(str, argv[1], 99);
-
printf("Input parameter = %s\n", str);
-
-
int count = atol(argv[2]);
-
-
pound(count, str);
-
}
On line 23, you're passing the value of num, not it's address.
2 1928
On line 23, you're passing the value of num, not it's address.
Thanks a lot Markus. That fixed it perfectly.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: fudmore |
last post by:
Hello Everybody.
I have a Segmentation fault problem. The code section at the bottom
keeps throwing a Segmentation fault when it enters the IF block for the
second time.
const int...
|
by: I_have_nothing |
last post by:
Hi!
I am new in C. I try to use dynamical allocation fuction malloc( ) and
realloc( ).
I found something strange.
After several calling realloc( ), the malloc( ) will give me a
Segmentation...
|
by: Christian Christmann |
last post by:
Hi,
I'm wondering why this program does not crash with a segmentation
fault:
#include <malloc.h>
#include <string.h>
#include <stdio.h>
int main()
|
by: sam_cit |
last post by:
Hi Everyone,
I have the following program unit,
#include <stdlib.h>
int main()
{
char *p = (char*)malloc(100);
if(p==NULL)
|
by: weidongtom |
last post by:
Hi,
I've written the code that follows, and I use the function add_word(),
it seems to work fine
*before* increase_arrays() is called that uses realloc() to allocate
more memory to words. But...
|
by: Andrea |
last post by:
I wrote this code:
void * xmalloc (size_t size){
register void *value = OPENSSL_malloc(size);
if (value == 0)
printf("virtual memory exhausted");
return value;
}
int _chooseTSK(char*...
|
by: paragon.john |
last post by:
Hello all,
I am trying to read a file into some allocated memory as part of a
program and I am running into a problem. The program gets a
segmentation fault during fread. I have previously...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |