473,387 Members | 1,583 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

invalid type argument of ‘unary *’ (have ‘int’)

Siddarth777
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. main()
  3. {
  4. int *a=5;
  5. int **p=&a;
  6. fflush(stdin);
  7. printf("%u,%u,%d",p,*p,**p);
  8. }
getting compiled perfectly but am getting
"segmentation fault" in runtime
please help
Sep 18 '10 #1

✓ answered by Siddarth777

thanks a lot for the reply
i have a doubt
in the above program in the statement
int *a=5
does that symbolizes implicitly that a value is holding the memory location of 5?
i dint understand what is invalid about that......as am a newbie in c programming pointers concept
please help

4 28920
Banfa
9,065 Expert Mod 8TB
The problem is that the pointer a does not point anywhere valid.

You say it compiles perfectly, do you get no compiler warnings? If not you probably don't have you compiler warning level set high enough.

The reason I say this is that on line 4 you assign an int to an int *. This effectively makes a point at memory location 5, almost certainly not a valid location on your system.

p points to a

So when you try to print

p is valid, it is the address of a
*p is valid it is a, the address of an int
**p is not valid it is the thing that a points to, that is memory location 5, not the address of an allocate int and you get a segmentation fault (a memory address fault).

You can not take the address of a numerical constant like 5.
Sep 18 '10 #2
thanks a lot for the reply
i have a doubt
in the above program in the statement
int *a=5
does that symbolizes implicitly that a value is holding the memory location of 5?
i dint understand what is invalid about that......as am a newbie in c programming pointers concept
please help
Sep 18 '10 #3
Banfa
9,065 Expert Mod 8TB
int *a=5;

does explicitly set the value of a to (the memory location) 5. There is nothing wrong in that apart from being a little strange.

The problem comes when you try to dereference a with *a, you try to access the memory location contained in a, in this case 5 and that is not a valid memory location, or a least one you do not have access rights to.
Sep 18 '10 #4
donbock
2,426 Expert 2GB
Your code has a point to whatever int is stored at address 0x0005. The segmentation fault occurs because the operating system has not granted your program permission to access this address. In addition, you might get a run-time alignment fault if your program runs on one of the many processors that don't allow ints to be at odd addresses.

Do you intend for a to point to an int whose value is 5? If so, then you want something like this:
Expand|Select|Wrap|Line Numbers
  1. int five = 5;
  2. int *a = &five;
Sep 18 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Kevin Grigorenko | last post by:
Hello all, Okay, I've got a templated class that that takes an int and a char *, but when I try to instantiate an object of that template, VS.NET complains with: error C2975: 'S' : invalid...
2
by: CoolPint | last post by:
As a self-exercise, I am trying to write a generic Priority Queue, which would store any type and and accept any user-definable "priority" function. After much tinkering, I came up with...
19
by: Lucas Machado | last post by:
i'm doing some Linux Kernel hacking for a course i'm currently taking. there is a pointer to a struct (struct example_struct *ex_ptr) in a .c that i want to access in a system call. i defined a...
2
by: newbie29 | last post by:
I get this error "invalid type argument of '->'". It occurs when: if (num_process->t_pid == -1) { .... } I have defined the struct num_process in the same file as where the above is...
4
by: bob | last post by:
I have little C experience and am concurrently trying to tackle C and LKM's (a little too ambitious maybe) anyway here is the problem I'm having with an example module I found. static int...
3
by: Suyash Upadhyay | last post by:
Hello all, I was writing a code regarding offset of structure elements. struct a { struct b { int i; float f; char ch; }x;
5
by: Sheldon | last post by:
Hi Everyone, I have defined a function: struct Transient arrFromHdfNode(HL_NodeList *nodelist, struct Transient retv); and in the code: struct Transient arrFromHdfNode(HL_NodeList...
2
by: Barry | last post by:
The problem brought by one earlier post from comp.lang.c++ http://groups.google.com/group/comp.lang.c++/browse_thread/thread/bf636c48b84957b/ I take part of the question and reproduce the code to...
2
by: Stephanie Smith | last post by:
I always get this error message! Here's the code: #include <stdio.h> #include <stdlib.h> int main(void) { char *string; char length_string = 0; char buffer_for_string = 0;
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
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...
0
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
jinu1996
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.