473,382 Members | 1,386 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,382 software developers and data experts.

Segmentation error, running kali Virualbox

#include <stdio.h>

int main()
{
int i;

char char_array[5] = {'a', 'b', 'c', 'd', 'e'};
int int_array[5] = {1, 2, 3, 4, 5};

unsigned int hacky_nonpointer;

hacky_nonpointer = (unsigned int) char_array;

for(i=0; i<5; i++)
{ // Iterate through hacky_nonpointer with the sizeof(char).
printf("[hacky_nonpointer] points to %p, which contains the char '%c'\n",
hacky_nonpointer, *((char *) hacky_nonpointer));
hacky_nonpointer = hacky_nonpointer + sizeof(char);
}

// char_pointer = (void *) ((char *) char_pointer + 1);
// ==> inc by char* data_typ then conv back to void *

hacky_nonpointer = (unsigned int) int_array;

for(i=0; i<5; i++)
{ // Iterate through hacky_nonpointer with the size_of(int) func.
printf("[hacky_nonpointer] points to %p, which contains the integer %d\n",
hacky_nonpointer, *((int *) hacky_nonpointer));
hacky_nonpointer = hacky_nonpointer + sizeof(int);
}
}

root@Brien:~# gcc -g -o ./pointer_types5 pointer_types5.c
pointer_types5.c: In function ‘main’:
pointer_types5.c:12:21: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
pointer_types5.c:17:24: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
pointer_types5.c:24:21: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
pointer_types5.c:29:24: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
root@Brien:~# ./pointer_types5
Segmentation fault
root@Brien:~#
Apr 7 '15 #1
4 1341
ignore the comment b/w the two for loop
Apr 7 '15 #2
weaknessforcats
9,208 Expert Mod 8TB
You might read this. Then post again.

http://bytes.com/topic/c/insights/77...rrays-revealed
Apr 7 '15 #3
didnt help with what im trying to do, did you see an error?
im trying to assign the mem address to the unsigned var and your the type casting to display it and sizeof(data_type) to iterate through the array
Apr 7 '15 #4
weaknessforcats
9,208 Expert Mod 8TB
The code you posted compiles and runs on my machine using Visual Studio 2013.
Apr 7 '15 #5

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

Similar topics

9
by: questions? | last post by:
What's wrong with the following sudo-code? ****************************** # include <stdio.h> # include <string.h> char *test="I am the string you testing on"; int main(){ char *point;
2
by: zl2k | last post by:
hi, all I am using a 2 dimensioanl array implemented by vector<vector<long> >. When the row number grows to 8 and I am trying to insert a new row, I got the segmentation error. However, if I...
3
by: Hottejonas | last post by:
I have written a program in which I declare a double array as follows double *uMatrix; uMatrix = new double; And I make sure to free it in the last part of the main program delete uMatrix;...
1
by: qavac | last post by:
I have written some code which has the following structure main() { repeatfunc(); } repeatfunc() {
1
by: nt2xist | last post by:
I am trying to output uniform numbers into another file and it runs into segmentation faut on the bolded line below.. Maybe it runs out of memory?.? or is it just too much looping and functions...
2
by: razzmia | last post by:
This should be a quick response which will be very helpful to me. Thank you in advance to anyone that answers my question. I am already familiar with Java, and have programmed with it in my current...
4
by: John Doe | last post by:
segmentation error !!!! hi guys , i wrote this program to multiply two matrices (just the basic code without checkin 4 the condition n==p ) " #include<stdio.h> main() { int a,b,c; int...
30
by: JK | last post by:
Hi, I am getting segmentation error with below-mentioned code and I am not able to make out why. typedef struct sd { int ps; int cs; } st;
11
by: sagarbsa | last post by:
Hii, when I run this program using GCC there is no compilation error but I get segmentation error. Please help me #include<stdio.h> #include<string.h> int main() { FILE *fp; char...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...

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.